Linux file programming (standard C library)

Commonly used: fopen, fseek, fwrite, fread, feof, fclose

Others: fputc, fgetc

The difference between fopen and open of the standard c library:

For details, please refer to: https://www.cnblogs.com/NickyYe/p/5497659.html

1.

open: UNIX system call function (including LINUX, etc.), which returns a file descriptor (File Descriptor), which is the index of the file in the file descriptor table.

fopen: The C language library function in the ANSIC standard, which should call different kernel APIs in different systems. What is returned is a pointer to the file structure.

2.open is applied to UNIX system, so it has certain limitations. fopen comes from the standard c library, and can be used in the code environment written in c language, with good portability

3.open returns the file descriptor, and the file descriptor is an important concept under the UNIX system. All devices under UNIX are operated in the form of files. Such as network sockets, hardware devices, etc. Of course, including the operation of ordinary regular files (Regular File).

fopen is used to manipulate ordinary regular files (Regular File).

4. From the perspective of file IO, the former belongs to the low-level IO function, and the latter belongs to the high-level IO function. The simple distinction between low-level and high-level is: who is closer to the system kernel. Low-level file IO runs in kernel mode, and high-level file IO runs in user mode.

5.fopen has a buffer, and can complete the corresponding operations in the buffer, which can be less dependent on the switching between the kernel and the user. open communicates with the kernel in a way similar to instructions, and the speed of file operations has a certain impact

fopen

Prototype: FILE *fopen(const char *filename, const char *mode); 

const char *filename parameter: filename;

const char *mode parameter: the way to open the file;

Parameters for opening the file:

"r"           open text file read-only
"w"           create text file write only
"a"           Supplement, Create a file if it does not exist
"r+"          Open a text file for reading/Write
"w+"          Create a text file to read/Write
"a+"          Open or create a file supplement
"b"           binary file(Can be combined with each of the above)
"t"           text this file(default item)

sample code

#include <stdio.h>
#include <string.h>

int main()
{
        FILE *p = fopen("./frAw.txt","w+");
        printf("open success!\n");
        return 0;
}

 

fseek 

Same usage as lseek

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

Parameter introduction:

      fd is the corresponding value of the file

      offset is the offset

whence can choose: SEEK_SET (the cursor returns to the beginning of the line), SEEK_CUR (the cursor is at the current subscript), SEEK_END (the cursor goes to the end of the line)

 

 fread 

Function prototype:

size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

void *buffer : read the binary data in the file into the buffer;

size_t size parameter: the byte size of the basic unit to read, the unit is byte, generally the unit size of buffer buffer;

  • If the buffer buffer is a char array, the value of this parameter is sizeof(char);
  • If the buffer buffer is an int array, the value of this parameter is sizeof(int);

size_t count parameter: the number of reads

FILE *stream parameter: pointer to the file

size_t return value: the number of reads

fwrite

Function prototype: size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)

Parameter Description:

const void *ptr : where the read data is stored

size_t size: the byte size of the basic unit of data to be written, the size of the unit to be written;

size_t nmemb : the number of times to read out the data;

FILE *stream: pointer to the file to read;


Return value: size_t The return value returns the readout times

sample code

write a string to a file, and read a string from a file

#include <stdio.h>
#include <string.h>

int main()
{
        char *str="xwd yyds!";
        char buf[64]={0};
        FILE *p = fopen("./frAw.txt","w+");
        fwrite(str,sizeof(char),strlen(str),p);
        fseek(p,0,SEEK_SET);
        fread(buf,sizeof(char),strlen(str),p);
        printf("read info:%s\n",buf);
        return 0;
}

 

At the same time, it can not only write and read strings, but also write and read other content. Let's take the structure as an example

sample code

#include <stdio.h>
#include <string.h>

struct Student{
        char name[15];
        int age;
};
int main()
{
        struct Student s1={"xiaoming",18};
        struct Student s2;
        FILE *p = fopen("./frAw2.txt","w+");
        fwrite(&s1,sizeof(struct Student),1,p);
        fseek(p,0,SEEK_SET);
        fread(&s2,sizeof(struct Student),1,p);
        printf("s2 info:%s,%d",s2.name,s2.age);
        fclose(p);
        return 0;
}

 

Garbled characters may appear when viewing the file content after successful execution, because the text is recorded in character strings

feof

Function prototype: int feof ( FILE * stream );

 FILE * stream : file pointer

Function: Judging whether the file content is over (end)

      Return 0 before the end, return 1 if there is no content after the end of the content

 

fputc 

Function prototype: int fputc(int char, FILE *stream)

int char : character

Parameter FILE *stream: pointer to the file

Generally, the content written to the file is moved by the pointer

sample code

 

#include <stdio.h>
#include <string.h>

int main()
{
        FILE *fp = fopen("./fputc.txt","w+");
        char *str = "xwd yyds!!";
        int i;
        int len = strlen(str);
        for(i=0;i<len;i++)
        {
                fputc(*str,fp);
                str++;
        }
        fclose(fp);
        return 0;
}

 

 

 

fgetc

Function prototype: int fgetc(FILE *stream)

Parameter FILE *stream: pointer to the file

Generally, the content of the file is obtained by moving the pointer

sample code

#include <stdio.h>
#include <string.h>

int main()
{
        FILE *fp = fopen("./fputc.txt","r");
        char c;
        while(!feof(fp))
        {
             c = fgetc(fp);
             printf("%c",c);
        }
        fclose(fp);
        return 0;
}

 

 

 


 

 

Tags: C Linux

Posted by neogemima on Sun, 08 Jan 2023 20:53:02 +0300