0

Hi.

The objective os to open a file and print 55 lines of content at a time. Since I am still learning 'C', my code (thus far) may appear slightly neophytic ;)

#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000

void pause( void );

int main( void ) {

   FILE* fp;
   int count, line = 0;
   char buf[SIZE];

   if( ( fp = fopen( "data.txt", "r" ) ) == NULL ) {
      fprintf( stderr, "Error opening file.\n" );
      exit( 1 );
   }

   fgets( buf, SIZE, fp );

   fclose( fp );

   for( count = 0; count != EOF; count++, line++ ) {
      while( line % 55 != 0 )
         printf( "%c\n", buf[count] );

      pause();
   }

   return 0;
}

void pause( void ) {

   char input[5];
   printf( "Press return when ready..." );
   gets( input );
}

Thanks.

4
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by dwks
0

what is this? what do you want us to do with this? I'm pretty sure this doesn't work.

0

you need to code it something like this -- it has to call fgets() for every line in the file. fgets() only reads one line of the file, not an array of lines.

while( fgets(buf, sizeof(buf), fp) != NULL)
{
   // now do something with this line
}
0
while( line % 55 != 0 )

Note that 0 % 55 is 0, so you'll pause before printing anything, and then display 55 lines ad infinitum. The best solution is probably to use something like this:

while( line != 0 && line % 55 != 0 )
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.