Read txt file and print contents numbering each new line of text
Alright, I have written the following code but i am stuck. Here is the problem and below is the code ive written thus far:
Write a program that will prompt the user for a file name and open that file for reading. Print out all the information in the file, numbering each new line of text.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int main()
{
char line[81], filename[21], c;
int i = 1;
FILE *inFile;
printf("\nPlease enter the name of a file: ");
gets(filename);
inFile = fopen(filename, "a+");
if (inFile == NULL)
{
printf("\nThe file %s was not successfully opened.", filename);
printf("\nPlease make sure the specified file exists.");
getch();
exit(1);
}
while (fgets(line, 256, inFile) != NULL)
printf("\n%s", line);
rewind(inFile);
printf("\nFile contents all uppercase: \n");
while ((c = fgetc(inFile)) != EOF) {
printf("\n%d", i)
putchar(toupper(c));
i++;
}
fclose(inFile);
getch();
return 0;
}
Thanks for the help guys.