Hi all, i've been working on this problem all morning (i'm a complete newb to C and only did it as part of my engineering course)
The question reads :
"Write a complete program to count the number of characters in a text file. The program should open a pre-existant text file (checking if the open operation was succesful), count the number of characters in the file (use a variable called numChars) and close the file afterwards.
Heres my attempt thus far
Code:
#include <stdio.h>
int main () {
int numChars;
FILE *Textfile;
Textfile = fopen("C://list.txt", "r");
if (Textfile == NULL)
{
printf ("File failed to open.\n");
} else {
while ( (Textfile = getchar() ) != EOF )
{
numChars++;
}
}
fclose(Textfile);
printf("The %d of characters in the text file is:", numChars);
return 0 ;
}
It seems to be mostly fine but problems are arrising in the while loop section, i think its due to the fact im using the pointer Textfile but am not entirely sure there could be a lot more problems knowing me. I also think i have to declare or include the txt file would i be right in thinking that ? Thanks in advance.
James