Printing data to terminal

Hi all,

I am trying to print the log file information to the terminal and I am using the below code to do so. But here I am able to print to the terminal only after log file generation is complete and with this getting some delay in the terminal.

But I want to log to the terminal in the real time. As data to the log file is getting printed, It should reflect to terminal as well in real time. Please let me know how can I modify the below code.

Suggestions are appreciated.
Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     FILE * xFile;
     char buffer [100];
     xFile = fopen ( path to file, "r");

     if (xFile == NULL)
     {
         perror ("Error opening log file to print to output stream");
     }
     else
     {
     while ( ! feof (xFile) )
     {
        if ( fgets (buffer , 100 , xFile) == NULL ) break;
        fputs (buffer , stdout);
     }
      fclose (xFile);
    }
Hello Shruthi LS,


But I want to log to the terminal in the real time. As data to the log file is getting printed


Then line 14 should be where you write to the log file.

In the if statement you should either "return 1" or "exit(1)" or find another way not to continue with the program if the file stream did not open.

Then the "else" will not be needed.

Without the rest of the code it makes it hard to see what you are doing completely.

Andy
Thank you so much for the suggestion @Handy Andy
Topic archived. No new replies allowed.