Linux system IO function

1. open function

  • Function prototype:
int open(const char *pathname,int flags);
int open(const char *pathname,int flags,mode_t mode);
  • pathname: the relative or absolute path of the file
  • flags: open mode
    • Required (the three methods are mutually exclusive)
      • O_RDONLY: read only open
      • O_WRONLY: write only open
      • O_RDWR: read / write open
    • Optional
      • O_APPEND: indicates append, which is added to the end of the file and does not overwrite the original data
      • O_CREAT: if this file does not exist, create it. The third parameter mode: indicates the access permission of the file
        • The file permission is determined by the open mode parameter and the umask mask of the current process: Mode & ~ umask
      • O_EXCL: if O is also specified_ Creat, and the file already exists, an error is returned
      • O_TRUNC: if the file already exists, Truncate its length to 0 bytes
      • O_NINBLOCK: set the file to non blocking state
  • Common errors:
    • Open file does not exist
    • Punch in the read-only file by writing (open the file without corresponding permission)
    • Open directory in write only mode

code:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(){
	//Open a file test
	int fd = open("test",O_RDWR | O_CREAT,0777);
	if(fd == -1){
		perror("open error");
		exit(1);
	}
	close(fd);
	return 0;
}

2. close function

  • Parameter: return value of open function
  • Return value
    • 0: normally closed
    • -1: Error closing

3. read function

  • Read data from an open device or file
  • Function prototype: ssize_t read(int fd, void *buf,size_t count);
    • fd: file descriptor
    • buf: data buffer
    • count: the number of bytes requested to be read
  • Return value
    • -1: Mistake
    • Greater than 0: bytes read
    • Equal to 0: file read completed

4. write function

  • Write data to an open device or file
  • Function prototype: ssize_t write(int fd,const void *buf.size_t count);
  • Return value:
    • -1: Fail
    • Greater than or equal to 0: the number of bytes written to the file
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <fcntl.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int main(){
	//Open file
	int fd = open("test.txt",O_RDWR);
	cout << "fd = " << fd << endl;

	//Open another file for writing
	int fd1 = open("temp",O_RDWR | O_CREAT,0664);
	cout << "fd1 = " << fd1 << endl;

	//read
	char buf[1024];
	int len = read(fd,buf,sizeof(buf));
	while(len > 0){
		//Write data to file
		int ret = write(fd1,buf,len);
		cout << "ret = " << ret << endl;

		len = read(fd,buf,sizeof(buf));

	}

	close(fd);
	close(fd1);
	return 0;
}

5. lseek function

  • Function prototype: off_t lseek(int fd,off_t offset,int whence)
    • int fd: file descriptor
    • off_t offset: offset
    • Int where: offset position
      • SEEK_SET: offset backward from file header
      • SEEK_CUR: offset backward from current position
      • SEEK_END: offset backward from the end of the file
  • Return value
    • Failure Return - 1
    • The offset backward from the start of the file
    • It is allowed to exceed the offset set at the end of the file, and the file will be expanded accordingly
  • application
    • Move the file pointer to the header: lseek(fd,0,SEEK_SET); The return value is 0
    • Get the current position of the file pointer: lseek(fd,0,SEEK_CUR);
    • Get file length: lseek(fd,0,SEEK_END);
    • Expand file space:
      • eg: the source file is 10K, and now it is expanded to 110k
      • lseek(fd,100,SEEK_END); Expand from the tail with an offset of 100
      • write(fd,"a",1); A write operation must be performed to succeed. At this time, the file size is 111k
#include <unisted.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <fcntl.h>
#include <iostream>
using namespace std;

int main(){
	int fd = open("test.txt",O_RDWR);
	if(fd == -1){
		perror("open error");
		exit(1);
	}
	//Document expansion
	int len = lseek(fd,100,SEEK_END);
	write(fd,"a",1);
	cout << "len = " << len << endl;
	close(fd);
	return 0;
}

6. perror function and global variable error

  • Function definition: void perror(const char *s);
  • explain
    • Used to output the cause of the error in the previous function to the standard device (stderr)
    • The string indicated by parameter s will be printed first, followed by the error reason string
    • The error reason determines the string to be output according to the value of the global variable error

      error
  • Global variables, defined in the header file errno H medium
  • Is the last error code of the recording system. The code is an int value
  • Each value corresponds to the error type represented by a string
  • When there is an error in calling some functions, the function will reset the value of errno

reference resources

Dark horse programmer Linux server development tutorial

Keywords: C++ Linux

Added by mckinney3 on Wed, 09 Feb 2022 01:08:48 +0200