I have a text file, which has random amount of characters, numbers, spaces and new lines. I'm trying to figure out how to find the "length" of this file. For example: if the text file contains "This is an example." the lenght should be 19. I tried using both sizeof(text_file) and strlen(text_file), but they don't give me the desired output.
What I also tried was this:
void test(FILE *f)
{
int i;
i=0;
char ch;
while( ( ch = fgetc(f) ) != EOF ) {
printf("%c",ch); /*This is just here to check what the file contains*/
if(ch!='\0') {
i=i+1;
}
}
printf("------------------\n");
printf("The length of the file is\n");
printf("%d",i); /*For some reason my length is always +1 what it actually should be*\
}
Is there an easier way to do this and why does the code above always give +1? I guess there is something wrong with the if statement, but I don't know what.
Any help is really appreciated thanks in advance.