To be continued

An overview of

  1. File content, mode and format; The second argument to fopen represents the read/write mode, fclose; File pointer;
  2. Getc and putc; Fprintf and fscanf; The fgets fputs; Rewind, ungetc; The fseek and ftell; Fgetpos fsetpos; Fread and fwrite; Feof and ferror; Randomly access the instance and write s_gets

The sample

  1. File content, mode and format; The second argument to fopen represents the read/write mode, fclose; File pointer;
  • There are two types of file contents, modes, and formats: binary and text. Binary files store binary data in memory, while text files are stored in the form of characters (one byte). Binary mode is read and written byte by byte, usingfreadfwriteFunctions; Text mode is read and written as characters and has many functions such as:scanfprintf,getc,putc,fscanf,fprintf,fgets,fputsAnd so on. UNIX and Linux have only one file format, which does not distinguish between binary and text\nFor line breaks, the file ends with statistics, which may not be the case on other operating systems, so there is a mapping when C reads the file in text mode (such as line breaks on the MAC)\rMapped to\n), and there is no such mapping as what is stored when read in binary mode.
  • aboutfopenThe first parameter of the function is the pointer to the target file, and the second parameter indicates the read/write mode, as shown below:rRead,wThat means empty write,aThat means append, add all three of them+Update mode, both read and write, pay attention to the distinction between write mode. And then for binary files, just add after those three lettersb(with+It doesn’t matter who comes first),For UNIX systems with only one file formatbAre all the same. The process of opening a file, do you know?FILEIt is a structure that contains information about the file, the buffer created for the file, and how much it is filled.
#include<stdio.h>

int main(int argc, char * argv[]){
    FILE * f = fopen("test.dat"."wb+");
    double d[3] = {0.1.4.5.6.8};
    printf("Successfully write %zd \n",fwrite(d,sizeof(double),3,f));
    double e[3];
    // Where is the file pointer?
    rewind(f);
    printf("Successfully read %zd \n",fread(e,sizeof(double),3,f));
    for(int i=0; i<3; i++){
        printf("%f ",e[i]);
    }
    fclose(f);
}   

// Result example
// Write three entries successfully
// Successfully read 3 entries
/ / 0.100000 4.500000 6.800000
Copy the code
  1. Getc and putc; Fprintf and fscanf; The fgets fputs; Rewind, ungetc; The fseek and ftell; Fgetpos fsetpos; Fread and fwrite; Feof and ferror; Randomly access the instance and write s_gets
int	 getc(FILE *);  // Return the ASCII value of the character on success, -1 (EOF) on failure
int	 putc(int, FILE *);  // Return the ASCII value of the character on success, -1 (EOF) on failure
int	 fprintf(FILE * __restrict, const char * __restrict, ...);
int	 fscanf(FILE * __restrict, const char * __restrict, ...);
char	*fgets(char * __restrict, int, FILE *);  // Return the first address of the string on success, 0 (NULL) on failure
int	 fputs(const char * __restrict, FILE * __restrict);  // Failure returns -1 (EOF)
void	 rewind(FILE *);   // go back to the top of the file
int	 ungetc(int, FILE *);  // Put a character back into the file stream
int	 fseek(FILE *, long.int);  // Specify the position of the file pointer, followed by the number of bytes (relative offset, see SEEK_CUR, SEEK_END, SEEK_SET)
long	 ftell(FILE *);  // Get the current file pointer location
int	 fgetpos(FILE * __restrict, fpos_t *);  // Make up for the fact that ftell bytes are 2 billion (long), or more
int	 fsetpos(FILE *, const fpos_t *);
size_t	 fread(void * __restrict __ptr, size_t __size, size_t __nitems, FILE * __restrict __stream);  // The address of the array, the size of the elements, the number of elements; Returns the number read on success, -1 (EOF) on failure
size_t	 fwrite(const void * __restrict __ptr, size_t __size, size_t __nitems, FILE * __restrict __stream);
int	 feof(FILE *);  // Check whether it is the end of the file or some other error after returning the EOF
int	 ferror(FILE *);
Copy the code
#include<stdio.h>

int main(int argc, char * argv[]){
    FILE * f = fopen("test.dat"."wb+");
    double d[3] = {0.1.4.5.6.8};
    printf("Successfully write %zd \n",fwrite(d,sizeof(double),3,f));
    double e[3];
    // Where is the file pointer?
    rewind(f);
    printf("Successfully read %zd \n",fread(e,sizeof(double),3,f));
    for(int i=0; i<3; i++){
        printf("%f ",e[i]);
    }
    printf("%d ",getc(f));  // EOF is returned
    if(feof(f)){  // Check if it is the end of the file
        printf("eof");
    }
    if(ferror(f)){  // Determine if there is an error
        printf("error");
    }
    fclose(f);
}   

// Result example
// Write three entries successfully
// Successfully read 3 entries
// 0.100000 4.500000 6.800000 -1 eOF
Copy the code
#include<stdio.h>
#include<string.h>
#define MAXSIZE 41
char * s_gets(char * str, int n);
int main(int argc, char * argv[]){
    char c[MAXSIZE];
    s_gets(c,MAXSIZE);
    fputs(c,stdout);
    char d = getchar();
    putchar(d);
}   
char * s_gets(char * str, int n){
    char * res;
    char * pos;
    res = fgets(str,n,stdin);
    pos = strchr(str,'\n');  // Key part!
    if(res){
        if(pos){
            *pos = '\ 0';
        }
        else{
            while(getchar() ! ='\n') {continue; }}}return res;
}

// Result example
// ./test
// ahgahgkjahdgjkahjgkahlkajhlkafjgklr agjakldg
// ahgahgkjahdgjkahjgkahlkajhlkafjgklr ah
// h                               
Copy the code

Random access instance, using the custom buffer function, parameters involving 0, 1, 2 (full buffer, line buffer, no buffer, _IOFBF, _IOLBF, _IONBF). int setvbuf(FILE * __restrict, char * __restrict, int, size_t);

The C library function int fflush(FILE *stream) flushes the output buffer of the stream, as long as the last action on the stream was not input.