home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C by Discovery (4th Edition)
/
C_By_Discovery_4th_Edition.tar
/
C_By_Discovery_4th_Edition
/
_DISK_
/
ch10
/
nl.c
< prev
next >
Wrap
C/C++ Source or Header
|
2005-06-16
|
1KB
|
39 lines
/* nl.c
*
* Synopsis - Opens a text file and copies the lines of the
* file to standard output with each text line
* preceded by a line number.
*
* Objective - To illustrate the use of fgets() for input and
* fputs() for output.
*/
/* Include Files */
#include <stdio.h>
#include <stdlib.h>
/* Constant Declarations */
#define NUMCHARS 512
int main( int argc, char *argv[] )
{
char inarray[NUMCHARS]; /* Note 1 */
int linecount = 1;
FILE *fp;
if ( argc < 2 ) {
printf( "Usage: nl filename\n" );
exit( 1 );
}
else if (( fp = fopen( argv[1], "r" )) == NULL ) {
printf( "Unable to open file %s.\n", argv[1] );
exit( 1 );
}
/* Note 2 */
while ( fgets( inarray, NUMCHARS, fp ) != NULL ) {
printf( "%d\t", linecount++ );
fputs( inarray, stdout ); /* Note 3 */
}
fclose( fp );
return 0;
}