943,929 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2835
  • C RSS
Jul 16th, 2008
0

Printing a certain number of lines from a file.

Expand Post »
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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 1000
  4.  
  5. void pause( void );
  6.  
  7. int main( void ) {
  8.  
  9. FILE* fp;
  10. int count, line = 0;
  11. char buf[SIZE];
  12.  
  13. if( ( fp = fopen( "data.txt", "r" ) ) == NULL ) {
  14. fprintf( stderr, "Error opening file.\n" );
  15. exit( 1 );
  16. }
  17.  
  18. fgets( buf, SIZE, fp );
  19.  
  20. fclose( fp );
  21.  
  22. for( count = 0; count != EOF; count++, line++ ) {
  23. while( line % 55 != 0 )
  24. printf( "%c\n", buf[count] );
  25.  
  26. pause();
  27. }
  28.  
  29. return 0;
  30. }
  31.  
  32. void pause( void ) {
  33.  
  34. char input[5];
  35. printf( "Press return when ready..." );
  36. gets( input );
  37. }

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
java_girl is offline Offline
6 posts
since Jul 2008
Jul 16th, 2008
0

Re: Printing a certain number of lines from a file.

what is this? what do you want us to do with this? I'm pretty sure this doesn't work.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jul 16th, 2008
0

Re: Printing a certain number of lines from a file.

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.
  1. while( fgets(buf, sizeof(buf), fp) != NULL)
  2. {
  3. // now do something with this line
  4. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jul 16th, 2008
0

Re: Printing a certain number of lines from a file.

  1. 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:
  1. while( line != 0 && line % 55 != 0 )
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to read .bmp, .jpeg image in C language?
Next Thread in C Forum Timeline: problems with string struct members





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC