//read function#include <stdio.h>#include<stdlib.h>#include<unistd.h>/*Stdin_fileno: Standard input, value is 0 Stdout_fileno: standard output, value is 1 Stderr_fileno: standard error, value is 2 header file is unistd.hread (file identifier, buffer, buffer size) function is the data that reads the buffer size from a file
ssize_t Read (int fd, void *buf, size_t count), read () has the advantage over the scanf () that reading () can read spaces, and scanf () encounters a space end but read cannot convert a numeric string directly to int type , the header file that needs to call the Atoi () function atoi () function is stdlib.h, and the function is to turn the string into a number*/intMainintArgChar*args[]) { Charbuf[ -]={0}; intnumx=0; intIndex=read (Stdin_fileno,buf,sizeof(BUF)); printf ("The return value is%d, and the input string is%s\n", INDEX,BUF); Numx=atoi (BUF); printf ("the number entered is%d\n", NUMX); return 0;}
//Write function#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>/*ssize_t Write (int fd, const void *buf, size_t count), write () function outputs buffer data to the specified file identifier*/intMainintArgChar*args[]) { Charbuf[ -]={0}; strcpy (BUF,"Fly with you!\n"); intindex=Write (Stdout_fileno,buf,strlen (BUF)); printf ("The return value is%d,\n", index); return 0;}
//Open function |close () function#include <stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<string.h>#include<errno.h>/*int Open (const char *pathname, int flags); Opne tries to open a file in the parameter pathname. The parameter flags specifies how the file is accessed. Flags must be set to O_rdonly,o_wronly,o_rdwr,o_creat,o_append to read-only, write-only, read-write, and create, append, if the file exists. When open succeeds, a file descriptor is returned. Open fails to return 1 and sets the errno variable errno is int, which indicates error number, header file is errno.h strerror () function is print error message, header file is String.h*/intMainintArgChar*args[]) { if(arg<2) {printf ("Please enter a parameter!\n"); return 0; } intFd=open (args[1],o_rdonly); if(fd==-1) {printf ("error message:%s\n", Strerror (errno)); }Else{printf ("The file identifier is%d\n", FD); Charbuf[ -]={0}; //reads the contents of the current fileRead (Fd,buf,sizeof(BUF)-1); printf ("%s\n", BUF); Close (FD); } return 0;}
Linux File Management