In the actual development process, often use files, such as print log, record the text of the data, debug debugging information, and so on, so Linux file operation is as a Linux developer one of the necessary skills.
1. Create
Everything needs to go through a process from scratch, Linux files are no exception, Linux under the creation of files There are many ways, the following list
(1). Create in shell with commands
For example: Touch hello.txt//Create a blank file named Hello.txt
(2). Creating with code in the program
A. int creat (const char *filename, mode_t mode) //filename need to specify a specific path, mode specifies the permissions of the file
If you need to see detailed instructions, you can use the command man creat in Linux
Example: Create ("./hello.txt", S_IXUSR); Creates a hello.txt file under the current path, and the user has read/write/execute permissions on the file
The function returns the file descriptor, and returns 1 indicates that the error occurred
B.int Open (const char *pathname, int flag, mode_t mode)
The flag needs to be set to o_creat, which means that if the file does not exist, create the file, mode is the same as the mode in the Creat function, it represents the permission property of the file, and it should be noted that if the O_creat parameter is added, the function returns an error if the file already exists.
2. Open File
int open (const char *pathname, int flags); Manipulating files that already exist
int open (const char *pathname, int flags, mode_t mode); Operation of files that do not exist, mode is mainly to set the file permissions
In Linux, man Open may not be able to find the introduction of the open function, because this is the man manual put it and creat together, direct man creat on the line, as shown above is to use this function to add the header file
The open function returns a file descriptor that needs to be used when calling Read/write in a program, in fact telling the program how to find the file.
3. Close the file
Close the file is simple, call the close () function directly, the argument is the file descriptor returned by the Open function
4. Read the contents of the file
int read (int fd, const void *BUF, size_t length); Reads the data from the current cursor position, and if you do not set the cursor position, it is the default to read length bytes from the beginning
5. Writing information to a file
int write (int fd, const void *BUF, size_t length); Writes data from the beginning of the cursor, by default writing from the beginning
6. Adjust the location of the file cursor
int Lseek (int fd, offset_t offset, int whence);
The Lseek function can shift the cursor relative to the current position, and the whence parameter can specify the current position (Seek_set: file at the beginning of the Seek_cur: file current cursor position seek_end: End of file), different parameters have different use for offset
Offset_set:
The offset is set to offset bytes. Offset relative to the file header
Offset_cur:
The offset is set to it's current location plus offset. Offset relative to the current position, negative number is forward, positive is backward
Offset_end:
The offset is set to the size of the file plus offset bytes. File size Plus offset value
function execution successfully returns offset relative to file header, failure returns-1, see Man Manual
Here's a simple little example:
1#include <stdio.h>2#include <sys/types.h>3#include <fcntl.h>4#include <sys/stat.h>5#include <string.h>6 7 #defineLENGTH 1008 9 intMain ()Ten { One intFD, Len; A CharStr[length]; - Char*tmp ="Hello World"; - theFD = open ("./hello.txt", O_creat | O_rdwr | S_IRUSR |s_iwusr, S_IRWXG); - if(FD <0) - { -printf"Open failed. \ n"); + return-1; - } + Else A { at Write (FD, TMP, STRLEN (TMP)); - } -Lseek (FD, Seek_set,sizeof(TMP)); - Write (FD, TMP, STRLEN (TMP)); - Close (FD); - return 0; in}View Code
Linux file operations