Standard IO interface functions setbuf fprintf sprintf sscanf fscanf fgetc fgets fopen freopen function 5.12

setbuf

/*================================================ ==
* File name: setbuf_demo.c
* Created by: memories
* Creation date: May 09, 2023
*   describe:
===================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{<!-- -->
    /*
     * stdout: standard output stream
     * stdin: standard input stream
     * stderr: standard error output stream
     *
     * printf() is a line buffer function, only when '\
', fflush, buffer is full, the program will end and output
    */
#if 0
    printf("hello world\
");
    printf("------------");
    fflush(stdout); // Manually flush the standard output stream
#elif 0
    printf("hello world\
");
    printf("------------");
    printf("\
");//The buffer of the entire standard output stream is refreshed and displayed to the terminal and displayed in a new line
#elif 0
    setbuf(stdout, NULL);//Set the buffer of the standard output stream to NULL, close the line cache, but the system will automatically refresh the buffer once
    printf("------------");
#elif 1
    char buf[32]={<!-- -->0};
    setbuf(stdout,buf); //Set the buffer of the standard output stream to the buf address space

    setbuf(stdout, NULL);//Close the cache of the standard output stream, print hello world, indicating that the buffer of the standard output stream always exists, but the data in the stream is copied to the buf buffer instead of
    printf("hello world");//Since the buffer is set to buf, the printf data is output to buf
    
#endif


    while(1);//The blocker ends, the system refreshes the cache immediately
    return 0;
}
hqyj@ubun

fprintf sprintf

/*================================================ ==
* File name: fprintf_demo.c
* Created by: memories
* Creation date: May 09, 2023
*   describe:
===================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{<!-- -->
    char buf[32]="hello world";
    printf("buf: %s\
",buf);//Output the data to the standard output stream by default
#if 0
    // Output the data to the specified stdout stream in a certain format
    fprintf(stdout,"buf: %s\
",buf);
#elif 1
    //Verify stderr without cache, you can output unconditionally whatever you give
    fprintf(stderr,"buf: %s\
",buf);
#endif
    int a=12,b=13;
    float c=3.14;
    printf("a: %d, b: %d, c: %.2f\
", a, b, c);

    char s1[32]={<!-- -->0};
    // Output the data to the specified buffer in a certain format, and do not have the ability to print to the terminal
    sprintf(s1,"a: %d, b: %d, c: %.2f",a,b,c);
    //Then print out s1
    printf("s1: %s\
",s1);


    while(1);
    return 0;//All caches will be automatically refreshed at the end of the program
}

fscanf sscanf

/*================================================ ==
* File name: fscanf_demo.c
* Created by: memories
* Creation date: May 09, 2023
*   describe:
===================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{<!-- -->
    int a, b;
    float c;
#if 0
    //From the standard input stream, input data to a, b, c in the following format
    scanf("%d%d%f", &a, &b, &c);
    printf("a=%d b=%d c=%.2f\
",a,b,c);
#elif 1
    // Input the data in the buffer into the three variables a, b, and c according to a certain format
    char buf[32] = "a=12 b=13 c=3.14";
    sscanf(buf,"a=%d b=%d c=%f", & amp;a, & amp;b, & amp;c);//write according to the format
    printf("a=%d b=%d c=%.2f\
",a,b,c);
#endif
    char N[32];
    char p[32];
    char s1[32]="N:liweip:18281232";
    sscanf(s1,"N:\p:%s",N,p);
    printf("N:%s p:%s\
",N,p);
    return 0;
}

fopen freopen

/*================================================ ==
* File name: fopen_demo.c
* Created by: memories
* Creation date: May 09, 2023
*   describe:
===================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{<!-- -->
    //1. Open or create an open file file.txt
    FILE *fp = fopen("file.txt","r + ");
    if(fp == NULL)//judging whether the operation is wrong
    {<!-- -->
        perror("fopen");//Print error information and user information
        return -1;//error handling
    }

    char buf[32]={<!-- -->0};
    //From the file stream, input data to variables according to a certain pattern, which is to read the contents of the file
    fscanf(fp,"%s",buf);
    printf("buf: %s\
",buf);

    
    // Inputting data into the fp stream is writing data to the file (writing from scratch)
     fprintf(fp,"hello world");
     //Forcibly refresh the file stream (full cache)
     fflush(fp);
     
    return 0;





/*=================================================
* File name: freopen_demo.c
* Created by: memories
* Creation date: May 09, 2023
*   describe:
===================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{<!-- -->
    printf("hello world\
");
    //Redirect the output of the stdout standard output stream to the file.txt file, the file is opened in "w" mode, and all printf data will be written into file.txt in the future
    freopen("file.txt", "w", stdout);
    printf("welcome to chengdu\
");
    printf("welcome to chengdu\
");

    return 0;
}

}


/*=================================================
* File name: freopen_test_demo.c
* Created by: memories
* Creation date: May 09, 2023
*   describe:
===================================================*/
#include <stdio.h>
//How many file.txt files can be opened at most in the system
int main(int argc, char *argv[])
{<!-- -->
    int count=0;
    while(1)
    {<!-- -->
        FILE *fp=fopen("file.txt","r");
        if(fp==NULL)
        {<!-- -->
            break;
        }
        count + + ;
    }
    printf("%d\
", count);

    return 0;
}


fgetc fgets

/*================================================ ==
 * File name: fgetc_demo.c
 * Created by: memories
 * Creation date: May 09, 2023
 *   describe:
 ===================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{<!-- -->
    FILE *fp = fopen("file.txt","r");
    if(fp==NULL){<!-- -->
        perror("fopen");
        return -1;
    }
#if 0
    //The content of the file is subscripted in the disk
    int ret = getc(fp);//read the data of the first character
    printf("%d\
",ret);

    ret = getc(fp);//read the data of the second character
    printf("%c\
",ret);
#elif 0
    while(1)
    {<!-- -->
        int ret = getc(fp);
        if(ferror(fp)){<!-- -->//ferror() judges whether the fp stream pointer is wrong, if wrong, returns non-zero.
            perror("getc");
            break;
        }

        if(feof(fp))//feof(); Determine whether the fp stream pointer operation reaches the end of the file, if it returns non-zero
        {<!-- -->
            printf("the end\
");
            break;
        }
        printf("%c", ret);

    }
#elif 1

    while(!feof(fp))
    {<!-- -->
        int ret = getc(fp);
        if(ferror(fp))
        {<!-- -->
            perror("gestc");
            break;
        }
        printf("%c", ret);
    }
    printf("\
");
#endif
    return 0;
}



/*=================================================
* File name: getsc_demo.c
* Created by: memories
* Creation date: May 10, 2023
*   describe:
===================================================*/

**Use the fgets function to calculate the file size and the number of lines in the file**

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{<!-- -->
#if 0
    char buf[4]={<!-- -->0};
    char *p = fgets(buf,sizeof(buf),stdin);
    if(ferror(stdin))
    {<!-- -->
        perror("fgets");
        return -1;
    }
    if(feof(stdin))
    {<!-- -->
        printf("end\
");
        return -1;
    }
    printf("buf:%s\
",buf);
    printf("p:%s\
",p);
#elif 0
    FILE *fp= fopen("file.txt","r");
    if(fp==NULL)
    {<!-- -->
        perror("fopen");
        return -1;
    }

    char buf[7]={<!-- -->0};
    char *p = fgets(buf,sizeof(buf),fp);
    printf("buf=%s",buf);
    printf("buf=%s",fgets(buf,sizeof(buf),fp));//Start reading from the remaining data
#elif 1
    FILE *fp= fopen("file.txt","r");
    if(fp==NULL)
    {<!-- -->
        perror("fopen");
        return -1;
    }
    int i=0,j=0;
    while(1)
    {<!-- -->
        int x;
        char buff[32]={<!-- -->0};
        char *q= fgets(buff,sizeof(buff),fp);
        if(feof(fp))
        {<!-- -->
            printf("end\
");
            break;
        }
        if(ferror(fp))
        {<!-- -->
            perror("fgets");
                break;
        }
        x=strlen(q);
        j=j+x;
        i + + ;
        printf("%s",q);
    }
    printf("Number of lines=%d file size=%d\
",i,j);
    fclose(fp);
#endif
    return 0;
}