Ground Surface Strongest C Language Summary File (Continuous Update)

Posted by roshanjameer on Sun, 02 Jan 2022 17:11:39 +0100

Latest update: 2021.12. 24
Environment: CLion2021.3; 64-bit macOS Big Sur

14. Documents

14.1 What is a file

1. From the point of view of file function, it can be divided into program files and data files:
(1) Program files: source file (.c), target file (.obj), executable program (under windows.exe)
(2) Data file: The contents of the file are not necessarily the program, but the data read and written while the program is running, such as a file from which the program needs to read data or a file that outputs content.
2. File name: File name contains three parts: File path + File name trunk + File suffix

14.2 Why use files

Make data persistent.

14.3 Opening and closing of files

  1. File pointer: Each used file has a corresponding file information area in memory. Information used to store files File information area changes when the file changes, such as file name, file status, current location of the file, etc. This information is stored in a structure variable, which is declared by the system and named FILE. FILE type pointers are generally used to maintain this FILE structure variable, that is, file pointers, for different compilers. ILE types do not contain exactly the same content, but are much the same.

  2. Stream: Because of the different media used to store files, it is no doubt cumbersome to know the read and write rules of the target hardware in order to store data on the file. To solve this problem, the concept of stream is introduced between programs and hardware. Programs only need to read and write to streams, which complete the reading and writing of hardware. Programs do not need to care about this problem, and streams handle it. An operation file is essentially an operation file stream.

    As long as the C language program runs, it opens three streams by default:
    (1) stdin - Standard Input Stream - Keyboard
    (2) stdout - Standard Output Stream - Screen
    (3) stderr - Standard Error Stream - Screen
    All three streams are FILE*

  3. FILE* fopen( const char* filename, const char* mode )
    Int Fclose (FILE *stream), successfully returns 0, otherwise returns EOF, EOF is actually -1.

//  pf is a pointer variable to FILE-type data that enables pf to point to the file information area (a structure variable) of a file.
//  The file can be accessed through the information in the file information area, that is, the file associated with it can be found through the file pointer variable
    FILE* pf;
//  Opening and closing of files
//    pf = fopen("test.txt","r"); // The default path is relative in the cmake-build-debug directory
    pf = fopen("/Users/zhangsan/Projects/test.txt","r");//You can also write absolute paths
    if(pf == NULL)
    {//Failure to open a file returns a null pointer, so using money will determine if the open was successful
        perror("fopen");
        return 1;
    }
    //read file
    //Close File
    fclose(pf);
    pf = NULL;
  1. int fputc( int ch, FILE *stream )
    Write one character at a time to a file
    FILE* pf = fopen("test.txt","w");
    if(pf == NULL)
    {
        perror("fopen");
        return 1;
    }
//    Write File
    fputc('f',pf);
    fputc('u',pf);
    fclose(pf);
  1. Int fgetc (FILE *stream);// Successfully returns ASCLL code for a character, otherwise returns EOF
	FILE* pf = fopen("test.txt","r");
	//Suppose I have five characters in this file
    if(pf == NULL)
    {
        perror("fopen");
        return 1;
    }
//    Read files from files
    int ret = fgetc(pf);
    printf("%c",ret);
    ret = fgetc(pf);
    printf("%c",ret);
    ret = fgetc(pf);
    printf("%c",ret);
    ret = fgetc(pf);
    printf("%c",ret);
    ret = fgetc(pf);
    printf("%c",ret);
    ret = fgetc(pf);//-1. Read the file and end it. Print the garbled code at the back
    printf("%c",ret);
    ret = fgetc(pf);
    printf("%c",ret);
    fclose(pf);
    pf = NULL;
  1. Writing data to standard output stream
    fputc('f',stdout);//Writing f to the standard output stream is essentially printing f on the console
    fputc('u',stdout);
  1. Read information from standard input stream
    int ret = fgetc(stdin);
    printf("%c",ret);
  1. Int fputs (const char* str, FILE* stream); Successfully returned a non-negative value, otherwise returned EOF
      FILE* pf = fopen("test.txt","w");
      if(pf == NULL)
      {
          perror("fopen");
          return 1;
      }
      //Write files on line
      fputs("sdada\n",pf);//Line breaks need to be reflected in the code
      fputs("jlsdjgha\n",pf);
      fclose(pf);
      pf = NULL;
  1. char* fgets( char* str, int count, FILE* stream );
 	FILE* pf = fopen("test.txt","r");
    if(pf == NULL)
    {
        perror("fopen");
        return 1;
    }
    //Read files by line
    char arr[10] = "xxxxxxxx";
    fgets(arr,4,pf);//Read 3 characters from pf (because you want to leave a place for\0) in arr
    printf("%s\n",arr);
    fgets(arr,4,pf);
    printf("%s\n",arr);
    fclose(pf);
    pf = NULL;
  1. Writing files to formatted data
    int fprintf( FILE* stream, const char* format, ... );
    The third parameter... is actually a variable length parameter, which means any number of parameters
typedef struct S {
    char arr[10];
    int num;
    float sc;
} S;
    S s = {"fudandaxue", 20, 3.14};
    FILE *ps = fopen("test.txt", "w");
    if (NULL == ps) {
        perror("fopen");
        return 1;
    }
    fprintf(ps, "%s %d %lf", s.arr, s.num, s.sc);//See structure s for data written to file test managed by ps. Txt
    fclose(ps);
    ps = NULL;
  1. Read files for formatted data
    int fscanf( FILE* stream, const char* format, ... );
typedef struct S {
    char arr[10];
    int num;
    float sc;
} S;
    S s = {0};
    FILE *pf = fopen("test.txt", "r");
    if (NULL == pf) {
        perror("fopen");
        return 1;
    }
    fscanf(pf, "%s %d %f", s.arr, &(s.num), &(s.sc));//From test.txt reads information into structure variable s
    fprintf(stdout, "%s %d %f", s.arr, s.num, s.sc);
    fclose(pf);
    pf = NULL;

The above reads and writes are all text, not binary, for all input and output streams, then binary, only for files

  1. size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
    Write the size of count in buffer to stream
	S s ={"asdf",20,3.14};
    FILE* pf = fopen("test.txt","w");
    if(NULL == pf)
    {
        perror("fopen");
        return 1;
    }
    //Write files in binary form, all but letters are scrambled, so to read binary written files, you need to use fread.
    fwrite(&s,sizeof(S),1,pf);
    fclose(pf);
    pf =NULL;
  1. size_t fread( void* buffer, size_t size, size_t count,FILE* stream );
    Read count size size information from stream into buffer
        S s[10] = {0};//It's easier to store with an array
    FILE *pf = fopen("test.txt", "r");
    if (NULL == pf) {
        perror("fopen");
        return 1;
    }
    //Read the file in binary form to read what fwrite writes in the correct format
    fread(s, sizeof(S), 2, pf);
    for (int i = 0; i < 2; ++i) {
        printf("%s %d %f", s[i].arr, s[i].num, s[i].sc);
    }
    fclose(pf);
    pf = NULL;

14.4 Sequential reading and writing of files

Random Read and Write of 14.5 Files

14.6 Text and Binary Files

14.7 File Read End Decision

14.8 File Buffer

Topics: C Back-end