简体   繁体   中英

C - file read don't print last line from file where i use multiple file to write

I wrote ac program which read from a input file and then print each line to the standard output but it don't print the last line of the file!

int main() {

   FILE *rf = fopen("input_text.txt", "r");


   char c;

   if (rf) {
      while ((c = getc(rf)) != EOF) {
          putchar(c);
      }
      fclose(rf);
   }

  return 0;
}

How can i fix this issue? Thanks in Advance!

You likely need to flush the output stream because it is being buffered. Add a call to fflush(stdout); just before fclose:

int main() {

   FILE *rf = fopen("input_text.txt", "r");


   int c;

   if (rf) {
      while ((c = getc(rf)) != EOF) {
          putchar(c);
      }
      fflush(stdout);
      fclose(rf);
   }

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM