I'm trying to use the fseek() and ftell() function to find the length of the file 'test.txt' which is present in the same directory as the file 'file.c'.
file.c
#include <stdio.h>
int main()
{
FILE *fp;
int len;
fp = fopen("test.txt", "r");
if(fp == NULL)
printf("Error opening file.");
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fclose(fp);
printf("The size of the file test.txt is: %d.\n", len);
return 0;
}
test.txt
abc def
There is no problem when I compile the file, but when I try to run it, I'm getting the 'Segmentation fault (core dumped)' error abd the execution terminates.
What's going wrong in here?