Beginner w/ C exercise

Would somebody be able to guide me through this exercise?

Write a program that displays the contents of a file at the terminal 20 lines at a time. At the end of each 20 lines, have the program wait for a character to be entered from the terminal. If the character is the letter q, the program should stop the display of the file; any other character should cause the next 20 lines from the file to be displayed.
First write a program that displays all of the lines of the file.
Okay I was able to display the file with the following code

#include <stdio.h>

int main()
{
FILE *ptr_file;
char buf[1000];

ptr_file =fopen("file.txt","r");
if (!ptr_file)
return 1;

while (fgets(buf,1000, ptr_file)!=NULL)
printf("%s",buf);

fclose(ptr_file);
return 0;
}
I suggest you use std::ifstream and std::getline for this.
If that works, then you must add conditions:
1
2
3
4
5
6
7
8
9
10
while ( fgets(buf,1000, ptr_file) != NULL )
{
  printf( "%s", buf );

  if ( /*we have printed 20 lines*/ )
  {
    // ask the user
    if ( /* input was 'q' */ ) break;
  }
}

Topic archived. No new replies allowed.