| | |
Printing a certain number of lines from a file.
![]() |
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 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
Thanks.
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

C Syntax (Toggle Plain Text)
#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.
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.
C Syntax (Toggle Plain Text)
while( fgets(buf, sizeof(buf), fp) != NULL) { // now do something with this line }
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.
c Syntax (Toggle Plain Text)
while( line % 55 != 0 )
c Syntax (Toggle Plain Text)
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
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
![]() |
Similar Threads
- Printing multiple pages, how? (C#)
- Using OpenGL in Visual C++: Part I (Game Development)
- Printing problem (VB.NET)
- Need Help With Simple 'c' Program!!! (C)
- Help Me On These Questions... (C++)
- Storing file input to an array? (C)
- Program, help (Java)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Help for Air Lines Reservition System (C)
Other Threads in the C Forum
- Previous Thread: How to read .bmp, .jpeg image in C language?
- Next Thread: problems with string struct members
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






