/*
Read in a text file and output another text file
that has the contents of the first text file with
line numbers
*/
#include <stdio.h>
int main()
{
char buffer[100]; // a variable for the strings we read in
int counter = 1; // used for line numbers
FILE *inputfile; // a pointer to the input file
FILE *outputfile; // a pointer to the output file
inputfile = fopen("c:\\test1.txt", "r"); // open text1.txt
// Before going on, make sure the input file opened properly
if(inputfile != NULL)
{
outputfile = fopen("c:\\test2.txt", "w"); // open text2.txt
// While there are lines in input file, read them in, append a number
// and output the results to outputfile
while (fgets(buffer, 100, inputfile))
{
fprintf(outputfile, "%d. %s", counter, buffer);
counter++;
}
// close the files
fclose(inputfile);
fclose(outputfile);
}
else
printf("Error opening input file.\n");
return 0;
}
|