Friday, 8 May 2015

Open() , Read() and Write() UNIX System Call

 #include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

int main()
{
char status_info[256];
int fd;
fd=open("junk.out", O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU);

if(fd==-1)
{
printf("File Could not opened");
return -1;
}
strcpy(status_info,"Welcome To Linux");
write(fd,status_info,sizeof(status_info));
close (fd);
strcpy(status_info,"\0");
fd=open("junk.out", O_RDONLY|O_CREAT);
read (fd, status_info, sizeof (status_info) - 1);
printf("%s",status_info);
}

Output:
   
[telnet29@linux ~]$ ./a.out

  Welcome To Linux

No comments:

Post a Comment