fseek() problems!

Feb 9, 2015 at 6:55pm
Hello, i wrote a little program with fseek. It works good till it reaches the first fseek(). Then it crashes and stops responding. Its really weird.
My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream inp("myfile.txt");

if(!inp.is_open()) return false;
inp.close();

FILE *f;
fopen("myfile.txt","rb");

fseek(f,0,SEEK_END); //there it crashes.

long s=ftell(f);
rewind(f);

fclose(f);
Feb 9, 2015 at 9:47pm
You don't initialize f. Combine lines 6 and 7 into
FILE *f = fopen("myfile.txt", "rb");
You don't need lines 1-4. Just check the return value from fopen instead:
if (f == NULL) return false;

Lastly, see if your operating system supposed stat(). If so then you can get the size of the file without even having to open it.
Feb 10, 2015 at 4:20pm
That works! Thanks!
Topic archived. No new replies allowed.