Using the Linux file API, you often see a thing called a file descriptor.
What is a file descriptor?
(1) The file descriptor is actually a number, this number in a process to express a specific meaning, when we open a file, the operating system in memory to build some data structures to represent the dynamic file, and then return to the application a number as a file descriptor, This number is tied to the data structures in our in-memory maintenance of this dynamic file, which we need to differentiate with this file descriptor later if we want to manipulate this dynamic file.
(2) A file descriptor is used to distinguish between multiple files opened by a program.
(3) The scope of the file descriptor is the current process, out of the current process this file descriptor is meaningless.
(4) File descriptor FD's legal range is 0 or a positive number, it cannot be a negative number
(5) Open return of the FD must be recorded well, later to this file all operations rely on this FD to correspond to this file, and finally close the file also need FD to specify close this file. If FD is lost before we close the file, then the file cannot be closed or read and written.
1) Open and read files
1#include <stdio.h>2#include <sys/types.h>3#include <sys/stat.h>4#include <fcntl.h>5#include <unistd.h>6 7 intMainintargcChar Const*argv[]) {8 9 intFD =-1;//File DescriptorTen One //Open File AFD = open ("Ghostwu.txt", O_RDWR); - - if( -1==FD) { theprintf"File open failed \ n"); -}Else { -printf"file opened successfully, fd=%d\n", FD); - } + - //Read File + intCount =0; A Charbuf[ -]; atCount = Read (FD, BUF, - ); - if( -1==count) { -printf"file read failed \ n"); -}Else { -printf"The file read succeeded, the number of bytes actually read is:%d\n content is%s\n", Count, buf); - } in - //Close File to Close (FD); + - return 0; the}
Need to exist in the current directory ghostwu.txt this file, otherwise open when the failure, there are 2 of APIs involved
int open (const char *pathname, int flags);
Open is very simple, the first parameter is the file path, the second is the file mode, in the Man manual also provides several other ways.
ssize_t Read (int fd, void *buf, size_t count);
The first parameter is a file descriptor, which is the value returned by open
The second parameter, buf, is used to store the content read from the file
The third parameter, which represents what you want to read from the file (note: This count number can be given casually, whichever is the actual number returned (the return value of Read)
2) Open and write files
1#include <stdio.h>2#include <sys/types.h>3#include <sys/stat.h>4#include <fcntl.h>5#include <unistd.h>6#include <string.h>7 8 intMainintargcChar Const*argv[]) {9 Ten intFD =-1;//File Descriptor One A //Open File -FD = open ("Ghostwu.txt", O_RDWR); - the if( -1==FD) { -printf"File open failed \ n"); -}Else { -printf"file opened successfully, fd=%d\n", FD); + } - + //Write a file A CharBuf[] ="I Love Linux, the Linux is very very good!!!"; at intCount =0; -Count =Write (FD, buf, strlen (BUF)); - if( -1==count) { -printf"file write failed \ n"); -}Else { -printf"The file is successfully written and the number of bytes actually written is:%d\n", count); in } - to //Close File + Close (FD); - the return 0; *}
ssize_t Write (int fd, const void *buf, size_t count);
The first parameter is a file descriptor, which is the value returned by open
The second parameter, buf, is used to store the content written
The third parameter, which indicates the size of the file you want to write
Linux system programming: Simple File IO operation