1. Create a file
int creat (const char *filename, mode_t mode)
FileName: The name of the file to be created (contains the path, the default is the current path)
Mode: Schema of the created file/access rights
Common patterns:
S_IRUSR readable
S_IWUSR can write
S_IXUSR Executable
S_irwxu readable, writable, and executable
You can also use numbers directly to represent the access rights of a file:
Executable 1
Can write 2
Readable 4
The and above three values, such as readable writable executable 7 (1+2+4)
No permissions 0
1#include <stdio.h>2#include <stdlib.h>3 4#include <sys/types.h>5#include <sys/stat.h>6#include <fcntl.h>7 8 voidCreate_file (Char*filename) { 9 Ten /*The properties of the created file 0755 indicate: One file owner readable writable executable; A user-readable executable of the group where the file owner resides; - other user-readable executable*/ - if(creat (filename,0755) <0){ theprintf"Create file%s failure!\n", filename); - exit (exit_failure); -}Else{ -printf"Create file%s success!\n", filename); + } - } + A intMainintargcChar*argv[]) { at inti; - if(argc<2){ -Perror ("You haven ' t input the filename,please try again!\n"); - exit (exit_failure); - } - in for(i=1; i<argc;i++){ - Create_file (Argv[i]); to } + - exit (exit_success); the}
2. Open File
int open (const char *pathname, int flags)
int open (const char *pathname, int flags, mode_t mode)
Common open Flags (flags):
O_rdonly read-only mode open
O_wronly Write-only mode open
O_RDWR Read and write mode open
O_append Append mode Open
O_create Creating a file
O_noblock non-blocking mode open
If you use the O_CREATE flag, you must specify mode to represent the file's access rights.
1#include <stdio.h>2#include <stdlib.h>3 4#include <sys/types.h>5#include <sys/stat.h>6#include <fcntl.h>7 8 intMainintARGC,Char*argv[]) {9 intFD;Ten if(argc<2){ OnePuts"Please input the open file pathname!\n"); AExit1); - } - the //If there is o_creat in the flag parameter, the file is created if it does not exist, and the file's permissions are determined by the third parameter, which is 0755 - //If there is no o_creat parameter in the Flah parameter, the third argument does not work. At this point, if the file you want to open does not exist, an error will be displayed. - //so Fd=open (ARGV[1],O_RDWR), just open the specified file - if((Fd=open (argv[1],o_creat| O_RDWR,0755)) <0){ +Perror ("Open File failure!\n"); -Exit1); +}Else{ Aprintf"Open File%d success!\n", FD); at - } - Close (FD); -Exit0); - -}
3. Close the file
int close (int fd)
FD: File descriptor (a non-negative integer)
4. Read the file
int read (int fd, const void *BUF, size_t length)
Reads the length bytes from the file that the file descriptor FD points to in the buffer pointed to by BUF, returning the actual number of bytes read.
5. Writing files
int write (int fd, const void *BUF, size_t length)
Writes the length byte in the buffer pointed to by the BUF to the file that the file descriptor FD points to, returning the actual number of bytes written.
6. Locate the file (move the file pointer)
int Lseek (int fd, offset_t offset, int whence)
Moves the file read-write pointer relative whence to offset bytes. If the operation succeeds, the position of the file pointer relative to the file header is returned.
Whence can take the following values:
Seek_set: Relative file start
Seek_cur: The current position of the relative file read-write pointer
Seek_end: Relative file end
Offset is a negative value that indicates moving forward.
7. Access Rights judgment
int access (const char *pathname, int mode)
Pathname: File name
Mode: To determine the access rights, you can take the following values or combinations of them. R_OK: File readable, W_ok: File writable, X_ok: File executable, F_OK: file exists.
Return value: When we test successfully, the function returns 0, otherwise if there is a condition that does not conform, returns-1.
1#include <sys/types.h>2#include <sys/stat.h>3#include <fcntl.h>4#include <stdio.h>5#include <errno.h>6 7 #defineBuffer_size 10248 9 intMainintargcChar**argv)Ten { One intfrom_fd,to_fd; A intBytes_read,bytes_write; - CharBuffer[buffer_size]; - Char*ptr; the - if(argc!=3) - { -fprintf (stderr,"usage:%s fromfile tofile/n/a", argv[0]); +Exit1); - } + A /*Open source File*/ at if((From_fd=open (argv[1],o_rdonly)) ==-1) - { -fprintf (stderr,"Open%s error:%s/n", argv[1],strerror (errno)); -Exit1); - } - in /*Create a destination file*/ - if((To_fd=open (argv[2],o_wronly| o_creat,s_irusr| S_IWUSR)) ==-1) to { +fprintf (stderr,"Open%s error:%s/n", argv[2],strerror (errno)); -Exit1); the } * $ /*The following code is a classic copy of the code of the file*/ Panax Notoginseng while(bytes_read=Read (from_fd,buffer,buffer_size)) - { the /*A fatal mistake happened.*/ + if((bytes_read==-1) && (ERRNO!=EINTR)) Break; A Else if(bytes_read>0) the { +Ptr=buffer; - while(bytes_write=write (to_fd,ptr,bytes_read)) $ { $ /*A fatal error has occurred.*/ - if((bytes_write==-1) && (ERRNO!=EINTR)) Break; - /*Write all the bytes read*/ the Else if(Bytes_write==bytes_read) Break; - /*just write a part and keep writing*/ Wuyi Else if(bytes_write>0) the { -ptr+=Bytes_write; Wubytes_read-=Bytes_write; - } About } $ /*A fatal error occurred while writing.*/ - if(bytes_write==-1) Break; - } - } A Close (FROM_FD); + Close (TO_FD); theExit0); -}
File operations (Linux system calls)