| | |
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 |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






