1_OPEN.C:
#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) { intFd//File Descriptor /*open a file as read and return an error if the file does not exist*/FD= Open ("Test.txt", o_rdonly); if(FD <0) {perror ("Open"); return-1; } printf ("FD = =%d\n", FD); return 0;}
2_OPEN.C:
#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) { intFd//File Descriptor /*open a file as read and create it if the file does not exist*/FD= Open ("Test.txt", O_rdonly | O_creat,0755); if(FD <0) {perror ("Open"); return-1; } printf ("FD = =%d\n", FD); return 0;}
3_READ.C:
#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) { intFd//File Descriptor intret; Charbuf[ -] = {0}; /*Read Standard terminal: 0 (Stdin_fileno)*/ret= Read (0, BUF,sizeof(BUF)-1); if(Ret >0){ /*Read Success*/ /*Print to Terminal*/printf ("%s\n", BUF); } return 0;}
4_READ.C:
#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>intMainintargcChar*argv[]) { intFd//File Descriptor intret; Charbuf[ -] = {0}; /*open a file to read the way*/FD= Open ("Test.txt", o_rdonly); if(FD <0) {perror ("Open Test"); return-1; } /*move the file pointer to the file header*/Lseek (FD,0, Seek_set); /*Read Standard terminal: 0 (Stdin_fileno)*/ret= Read (Fd,buf,sizeof(BUF)-1); if(Ret >0){ /*Read Success*/ /*Print to Terminal*/printf ("%s\n", BUF); } return 0;}
5_WRITE.C:
#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>intMainintargcChar*argv[]) { intFd//File Descriptor intret; Charbuf[ -] = {0}; /*open a file to read the way*/FD= Open ("Test.txt", o_rdonly); if(FD <0) {perror ("Open Test"); return-1; } /*move the file pointer to the file header*/Lseek (FD,0, Seek_set); /*Read Standard terminal: 0 (Stdin_fileno)*/ret= Read (Fd,buf,sizeof(BUF)-1); if(Ret >0){ /*Read Success*/ /*Print to Terminal*/Write (1, Buf,ret); } return 0;}
6_WRITE_READ.C:
#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>intMainintargcChar*argv[]) { intFdr//File Descriptor intFDW; intret; Charbuf[ -] = {0}; /*open a file to read the way*/FDR= Open ("Test.txt", o_rdonly); if(FDR <0) {perror ("Open Test"); return-1; } /*Open a file dest.txt, for write, if the file does not exist, create*/FDW= Open ("Dest.txt", O_wronly | O_creat,0755); if(FDW <0) {perror ("Open Dest"); return-1; } /*move the file pointer to the file header*/Lseek (FDR,0, Seek_set); /*Read Standard terminal: 0 (Stdin_fileno)*/ret= Read (Fdr,buf,sizeof(BUF)-1); if(Ret >0){ /*Read Success*/ /*write to File Dest.txt*/write (Fdw,buf,ret); } return 0;}
Operating instance of file I/O