Standard c library: fputc, fgetc, feof

Posted by kpulatsu on Fri, 04 Mar 2022 20:31:51 +0100

1,Understand the usage of standard c library fopen (click to jump)

2. fputc writes a character to the file

#include <stdio.h>

int main()
{
        FILE *fp;

        fp = fopen("./test.txt","w+");

        //int fputc(int c, FILE *stream);
        fputc('a',fp);

        fclose(fp);

        return 0;
}

3. fputc writes a string to a file

3.1. Error examples and small details.

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

int main()
{
        FILE *fp;
        char *str = "wenjian chu ru men !";
        int i = 0;

        fp = fopen("./test.txt","w+");

        for(i=0;i<strlen(str);i++){
                //int fputc(int c, FILE *stream);
                fputc(*str,fp);
                str++;                //Write to the file one character at a time
        }

        fclose(fp);

        return 0;
                                                                                        
}

Q: why can't you write it all in?

Because I < strlen (STR) is changing in the for loop. It's getting smaller.

3.2 correct examples

Calculate the length of the string before the for loop. This small detail can avoid incomplete writing.

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

int main()
{
        FILE *fp;
        char *str = "wenjian chu ru men !";
        int i = 0;

        int len = strlen(str);

        fp = fopen("./test.txt","w+");

        for(i=0;i<len;i++){
                //int fputc(int c, FILE *stream);
                fputc(*str,fp);
                str++;                //Write to the file one character at a time
        }

        fclose(fp);

        return 0;
                                                                                        
}

4,fgetc,feof 

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

int main()
{
        FILE *fp;
        char c;

        fp = fopen("./test.txt","r"); //Permission is given to "r" to open the file as read-only. The file must exist.

        while(!feof(fp)){
                c = fgetc(fp);
                printf("%c",c);
        }


        fclose(fp);

        printf("\n");

        return 0;
}

 

You can see that the result of the program output is one more character than that of txt file? Symbol.

4.1 solution

The following is an excerpt and a link to the original text: https://blog.csdn.net/weixin_43667437/article/details/108920944

After consulting relevant data, it is found that when the incoming parameter fp reaches the end of the file, the fgetc function will return an EOF, and the real value of this EOF is - 1.
So is it possible that the fgetc function outputs one more time? When the loop reaches the end of the file, it does not stop, but outputs the last EOF?

Add two lines of code to experiment:

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

int main()
{
        FILE *fp;
        char c;

        fp = fopen("./test.txt","r"); //Permission is given to "r" to open the file as read-only. The file must exist.

        putchar(-1);
        printf("\n");

        while(!feof(fp)){
                c = fgetc(fp);
                printf("%c",c);
        }


        fclose(fp);

        printf("\n");

        return 0;
}

 

-This is the output result of 1? Symbol, that is, the loop goes one more time before it ends, resulting in EOF being output as a character.
After consulting relevant materials, we get the following experience:

The judgment method of feof function is not to judge whether the current pointer points to the end of the file, but to judge according to the return value read last time. If the return value read last time is EOF, feof(fp) is true, otherwise it is false.

The method of fgetc function is exactly: first read the content pointed by the current pointer, and then move the pointer back.

In this way, if the two are operated together, one more EOF will be read:
When getc reads the last character of the txt document, fp points to the last character. After reading the character, fp moves backward and fp points to EOF for the next cycle. Feof judges whether it reaches the end of the document: since the last character of the document was read by fgetc last time, feof function will return false even if fp currently points to EOF, so the cycle continues, In the next cycle, the EOF output pointed to by fp will be moved back (it has reached the end of the document and cannot be moved back);

Modify the code again:

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

int main()
{
        FILE *fp;
        char c;

        fp = fopen("./test.txt","r"); //To open a file as read-only, it must exist.

        c = fgetc(fp);
        while(!feof(fp)){
                printf("%c",c);
                c = fgetc(fp);
        }

        fclose(fp);

        printf("\n");

        return 0 ;
}

The output result is normal
 

 

Topics: C Linux