Search This Blog

Showing posts with label open. Show all posts
Showing posts with label open. Show all posts

C Program to open, read and write files and perform file copy operation usingSystem Calls in UNIX

Write a program to open, read and write files and perform file copy operation. This program show how to open, read, write and copy files using Linux or Unix system calls like open(), read() and write().

#include<stdio.h>
#include<fcntl.h>
main()
{


char buff[1000],fn1[10],fn2[10];
int fd1,fd2,n;
printf("Enter source filename\n");
scanf("%s",fn1);
printf("Enter destination file name\n");
scanf("%s",fn2);
fd1=open(fn1,O_RDONLY);
n=read(fd1,buff,1000);
fd2=open(fn2,O_CREAT|O_WRONLY);
n=write(fd2,buff,n);
close(fd1);
close(fd2);
}

C Program to Open, Read and Write a file using System Calls

This is a C program to open , read and write files using system calls in Linux (UNIX) operating systems. The system calls open(), read() and write() are used in the C program to open, read and write files in Unix/Linux operating systems.

#include<stdio.h>
#include<fcntl.h>
main()
{


char buff[100],fn[10];
int fd,n;
printf("Enter file name\n");
scanf("%s",fn);
fd=open(fn,O_RDONLY);
n=read(fd,buff,100);
n=write(1,buff,n);
close(fd);
}