Linux C fgetc ()

Source: Internet
Author: User

Today, when practicing the code, I ran into a question like this:

A file test.txt, the file content is

1

2

4

5

Read and write this file in the program, modify its contents, add a line, and turn the contents of the file into:

1

2

3

4

5

The landlord's error code is this:

1#include <stdio.h>2#include <string.h>3 4 intMainintargcConst Char*argv[])5 {6FILE *fp,*FD;7fp = fopen ("Text.txt","R");8FD = fopen ("Text1.txt","W");9     CharA;Ten     intLine=0; One      while((A= (Char) fgetc (FP)) >0) A         { - FPUTC (A,FD); -             if(A = ='\ n') the             { -line++; -                 if(line==2) -                 { +FPUTC ('3', FD); -FPUTC ('\ n', FD); +                 } A             } at         } -//fflush (FP); -//fflush (FD); - Close (FP); - Close (FD); -printf"re\n"); in     if(fp = fopen ("Text.txt","W")) ==NULL) -     { toprintf"FP error\n"); +         return-1; -     } the     if(FD = fopen ("Text1.txt","R")) ==NULL) *     { $printf"FD errpr\n");Panax Notoginseng         return-1; -     } the      while((A = (Char) fgetc (FD)) >0) +     { Aprintf"enter\n"); the FPUTC (A,FP); +     } -printf"%d\n", a); $ Close (FP); $ Close (FD); -     return 0; -}

This compiles and runs, the content in Text1.txt is 1 2 3 4 5

But there will be no content in Text.txt. I worry for a while, then think of it, standard IO is a buffer, it should be the buffer here, in the program to add a flush buffer, the program is normal. (It is possible to remove the two comments from the above procedure.) )

The reason for this is that the standard IO Open file takes a full buffer, and the contents of the buffer are removed until the program finishes or the buffer is full or a manual refresh takes place.

When the Text.txt is first opened and read, the contents of the buffer are such that 1 \ n 2 \ 4 \ 5 \n-1 (here the negative one represents the end of the file, the specific explanation please man fgetc), after the IO stream is closed (not refreshed), in the open IO stream, the data in the buffer is still present, at this time in the read Take the data in the text1.txt, I guess the buffer data should be such 1\n 2\n 4\n 5\n-1, because before reading-1 o'clock did not read the data, the buffer position is still stuck at 1, the second read directly read the negative one then the loop will not be executed, You cannot assign data to Text.txt, and because Text.txt is write-open, there is no data in it.

Linux C fgetc ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.