reopened by
1,216 views
1 votes
1 votes

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?

reopened by

1 Answer

Related questions

0 votes
0 votes
0 answers
1
ykrishnay asked May 20, 2022
383 views
What is stream in c programming ? as i read file io in c programming so thats where i read “stream” word so what stream means ? thank you
1 votes
1 votes
1 answer
2
iarnav asked Apr 11, 2017
1,846 views
Hello, please kindly list out the topics which are needed to be studied from topic file system?!You can also pinpoint the topics from Galvin book 7th edition.
1 votes
1 votes
1 answer
4