Printing a certain number of lines from a file.

Reply

Join Date: Jul 2008
Posts: 6
Reputation: java_girl is an unknown quantity at this point 
Solved Threads: 0
java_girl java_girl is offline Offline
Newbie Poster

Printing a certain number of lines from a file.

 
0
  #1
Jul 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

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

 
0
  #2
Jul 16th, 2008
what is this? what do you want us to do with this? I'm pretty sure this doesn't work.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #3
Jul 16th, 2008
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. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

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

 
0
  #4
Jul 16th, 2008
  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 )
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC