Last night, I took the time to practice file I/O programming. Because embedded Linux is cropped by llinux, its system call and User Programming Interface APIs are basically the same as those of Linux, learning the basic programming of related content in Linux is very helpful for the actual development of embedded Linux in the future.
This article describes several basic file I/O operation functions, such as open, close, read, write, and lseek.
1. Open
Prototype: int open (const char * pathname, int flags, int perms)
Pathname indicates the name of the file to be opened, flags indicates the method of opening the file, and perms indicates the access permission. If the operation succeeds, the system returns the file descriptor (usually expressed in FD) and-1 if the operation fails. For specific parameters, refer to relevant documents.
2. Close
Prototype: int close (int fd)
FD is the file descriptor obtained in the preceding OPEN function. If 0 is returned successfully,-1 is returned when an error occurs.
Example:
Int FD; <br/> If (FD = open ("/tmp/jarvis_hello.c", o_creat | o_trunc | o_wronly, 0600) <0 ); <br/>{< br/> perror ("open"); <br/> exit (1 ); <br/>}</P> <p> If (close (FD) <0) <br/>{< br/> perror ("close "); <br/> exit (1); <br/>}
3. Read
Prototype: ssize_t read (int fd, void * Buf, size_t count)
Buf refers to the buffer zone for reading data from the memory, and count refers to the number of bytes read. 0 is returned. Otherwise,-1 is returned.
4. Write
Prototype: ssize_t write (int fd, void * Buf, size_t count)
Buf is the data buffer written to the specified storage, and count is the number of written bytes. The number of written words is returned successfully, and-1 is returned if the number of written words fails.
5. lseek
Prototype: off_t lseek (int fd, off_t offset, int whence)
Offset is the offset, which can be positive or negative (forward or backward). Whence has three parameters to choose from: Start of seek_set file, current position of seek_cur file, and end of seek_end file. Whence can be used with offset to determine the specific location of the file to be operated. If the execution is successful, the current file displacement is returned. If the operation fails,-1 is returned.
Example:
Int I, FD, size, Len; <br/> char * Buf = "Hello, I'm jarivs! "; <Br/> char buf_r [10]; <br/> Len = strlen (BUF); </P> <p> FD = open (" tmp/jarvis_hello.c ", o_creat | o_trunc | o_rdwr, 0666); // omitting Fault Tolerance detection </P> <p> If (size = write (FD, Buf, Len) <0) <br/>{< br/>... <br/> // error handling <br/>} // otherwise, set "Hello, I'm jarivs! "File written successfully </P> <p> lseek (FD, 0, seek_set); // because the file pointer position was changed during file writing, now it is restored to the start position </P> <p> If (size = read (FD, buf_r, 10) <0) <br/>{< br/>... <br/> // error handling <br/>} // otherwise, 10 bytes are read from the beginning of the file. </P> <p> close (FD ); <br/> ....... // omitted </P> <p>
If you are interested in learning Linux programming, it is not enough. At least, you have to break down the program in your own platform environment to see if it has achieved the expected results. The above is only an introduction to the most basic file I/O operation functions. For more information, I will share it with you in later learning records. If there are any errors or omissions, please refer to them.