Sams Teach Yourself C in 21 Days

(singke) #1
The First Type & Run ..........................................................................................

Enter and compile the following program. If you get any errors, make sure you entered
the program correctly.
The usage for this program is print_itfilename.ext, wherefilename.extis the
source filename along with the extension. Note that this program adds line numbers to
the listing. (Don’t let this program’s length worry you; you’re not expected to understand
it yet. It’s included here to help you compare printouts of your programs with the ones
given in the book.)

LISTINGT&R 1 print_it.c
1: /* print_it.c—This program prints a listing with line numbers! */
2: #include <stdlib.h>
3: #include <stdio.h>
4:
5: void do_heading(char *filename);
6:
7: int line = 0, page = 0;
8:
9: int main( int argv, char *argc[] )
10: {
11: char buffer[256];
12: FILE *fp;
13:
14: if( argv < 2 )
15: {
16: fprintf(stderr, “\nProper Usage is: “ );
17: fprintf(stderr, “\n\nprint_it filename.ext\n” );
18: return(1);
19: }
20:
21: if (( fp = fopen( argc[1], “r” )) == NULL )
22: {
23: fprintf( stderr, “Error opening file, %s!”, argc[1]);
24: return(1);
25: }
26:
27: page = 0;
28: line = 1;
29: do_heading( argc[1]);
30:
31: while( fgets( buffer, 256, fp ) != NULL )
32: {
33: if( line % 55 == 0 )
34: do_heading( argc[1] );
35:

26 Type & Run 1

04 448201x-T&R1 8/13/02 11:20 AM Page 26

Free download pdf