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.

Recommended Answers

All 3 Replies

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

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
}
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 )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.