File programming of Linux system programming ①

1. Opening and creating files

Function prototype:

 int open(const char *pathname, int flags);
    int open(const char *pathname, int flags, mode_t mode);

Parameter Description:

1. pthname: the name of the file to be opened (including the path, the default is the current path)

2. flags: O_RDONLY (read-only open), O_WRONLY (write-only open), O_RDWR (readable and writable open)

Only one of the above three parameters should be specified.

The following constants are optional:

O_CREAT Create the file if it does not exist. When using this option, you need to specify the third parameter mode at the same time, and use it to specify the access permission of the new file.

O_EXCL It is an error if O_CREAT is also specified and the file already exists.

O_APPEND is added to the end of the file each time it is written.

When opening a file with the O_TRUNC attribute, if the file originally has content and is successfully opened for read-only or write-only, its length will be truncated to 0.

3. mode: The O_CREAT flag must be used in flags, and mode records the access rights of the files to be created

demo.c

#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
int fd;
fd = open("./file",O_RDWR);

if(fd == -1)
{
printf("open the file fail\r\
");

fd = open("./file",O_RDWR|O_CREAT,0700);
if(fd > 0)
printf("open the file success\r\
");
}
        
    /*
        The return value of the open function
        Successfully returns a positive number greater than 0,
        Return -1 on failure
    */

close(fd);
return 0;
}

return result:

2. File write operation

Function prototype:

ssize_t write(int fd, const void *buf, size_t count);

Parameter Description:

1. fd: The descriptor returned after the file is opened and created is about to be written into the file

2. *buf: an untyped pointer, which is the data content to be written

3. count: the size of the content data to be written

demo.c

#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main()
{
int fd;
char *buf = "you are very handsome!!!";

fd = open("./file",O_RDWR);
if(fd == -1)
{
printf("open the file fail\r\
");

fd = open("./file",O_RDWR|O_CREAT,0600);
if(fd > 0)
printf("success\r\
");
}

int n_write = write(fd,buf,strlen(buf));
    
    /*
        write function return value
        Returns the size of the data written to the file
    */

printf("write %d byte in the file\r\
",n_write);
\t
close(fd);
return 0;
}

return result:

3. File read operation

Function prototype:

ssize_t read(int fd, void *buf, size_t count);

Parameter Description:

1. fd: the descriptor returned after the file is opened and created

2. *buf: an untyped pointer, which is the content to be written

3. count: read the size of the data in the file

demo.c

#include "stdio.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "you are very handsome!!!";

fd = open("./file",O_RDWR);
if(fd == -1)
{
printf("open the file fail\r\
");

fd = open("./file",O_RDWR|O_CREAT,0600);
if(fd > 0)
printf("open the file success\r\
");
}

int n_write = write(fd,buf,strlen(buf));

close(fd);
open("./file",O_RDWR);


char *readbuf = malloc(sizeof(char) * n_write + 1); //create memory dynamically

int n_read = read(fd,readbuf,strlen(buf));

    /*
        read function return value
        read file size
    */
printf("the file have %d byte, the content is %s\r\
", n_read, readbuf);

close(fd);
return 0;
}

return result:

4. File cursor movement operation

Function prototype:

off_t lseek(int fd, off_t offset, int whence);

Parameter Description:

1.fd: The file pointed to by the fd that needs to be moved

2.offset: is the offset value of cursor movement

3.whence: is the position of the cursor

SEEK_SET The position of the file header
SEEK_END the position of the tail of the file
SEEK_CUR The current position of the cursor before the default operation

demo.c

#include "stdio.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "you are very handsome!!!";
fd = open("./file1",O_RDWR);
if(fd == -1)
{
printf("open the file fail\r\
");

fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0)
printf("open the file success\r\
");
}

int n_write = write(fd,buf,strlen(buf));
printf("write %d byte in the file\r\
",n_write);


//off_t lseek(int fd, off_t offset, int whence);
lseek(fd,-(strlen(buf)),SEEK_END); //Set the position of the cursor to the beginning of the file


char *readbuf = malloc(sizeof(char) * n_write + 1);
int n_read = read(fd,readbuf,strlen(buf));
printf("the file have %d byte, the content is %s\r\
", n_read, readbuf);

close(fd);
return 0;
}

return result:

Note: The return value of lseek refers to the number of bytes offset by the file header, which is also very clever to calculate the size of the file

demo.c

#include "stdio.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "you are very handsome!!!";

fd = open("./file1",O_RDWR);

int cnt = lseek(fd,0,SEEK_END);
printf("the file have %d byte\r\
", cnt);

close(fd);
return 0;
}

return result:

5. Supplement for file open

O_EXCL If O_CREAT is specified at the same time, but the file already exists, an error will occur

demo.c

#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
fd = open("./file2",O_RDWR|O_CREAT|O_EXCL,0700);
\t\t
if(fd == -1)
{
printf("the file2 exist\r\
");
}

    /*
        Return -1 on error
    */

return 0;
}

Return result:

O_APPEND is added to the end of the file every time it is written

demo.c

#include "stdio.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "you are very handsome!!!";
fd = open("./file2",O_RDWR|O_APPEND);


int n_write = write(fd,buf,strlen(buf));
printf("write %d byte in the file\r\
",n_write);

close(fd);
return 0;
}

return result:

When opening a file with the O_TRUNC attribute, if the file originally has content and is successfully opened for read-only or write-only, its length will be truncated to 0

demo.c

#include "stdio.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "test";
fd = open("./file2",O_RDWR|O_TRUNC);


int n_write = write(fd,buf,strlen(buf));
printf("write %d byte in the file\r\
",n_write);

close(fd);
return 0;
}

return result:

6. File creation

int creat(const char *pathname, mode_t mode); 

Parameter Description:

1. *pathname: the name of the file to be created (including the path, the default is the current path)
2. mode: create mode

Common creation patterns:
S_IRUSR Readable

S_IWUSR Writable

S_IXUSR executable

S_IRWXU Readable, writable and executable

demo.c

#include "stdio.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "test";

//int creat(const char *pathname, mode_t mode);
fd = creat("./hello.txt",S_IRWXU);
if(fd > 0)
printf("create success\r\
");

return 0;
}

return result:

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. CS introductory skill tree Introduction to LinuxFirst-knowledge Linux28810 People are learning systematically