-1

So I open a file, go to the end to see how long it is, and then going back to the beginning causes a segmentation fault (core dumped). What the gosh?

...
FILE *passkey;
passkey = fopen("pass.key", "r+");
fseek(passkey, 0, SEEK_END);
filesize = ftell(passkey);
rewind(passkey);
...

The rewind causes a segmentation fault. It does the same with an fseek to the beginning. Why does it do this?

  • 8
    You **must** check returned values from all four functions that you used. Any one of them could have failed, and any failure could lead to a segfault. – DYZ Mar 02 '17 at 00:00
  • Have you checked you pass.key file exists? – soasme Mar 02 '17 at 00:02
  • I did that, everything works until the rewind. I tested all the values returned by these, and I put printf statements after all of these to test all of them, it is the rewind. – picklesrevil Mar 02 '17 at 00:03
  • How do you know what `fseek` returned if you do not even save the return value? – DYZ Mar 02 '17 at 00:04
  • I know that pass.key exists. I checked all of the functions listed iteratively, compiling one program for each function test. They all work besides rewind. – picklesrevil Mar 02 '17 at 00:06
  • 1
    post your actual code that tests all the return codes. If it is really failing after all these things are OK then probably you already UB'd earlier in your program – pm100 Mar 02 '17 at 00:10
  • `fopen` can also fail if you don't have appropriate permissions for your desired operation (in this case read and write). Are you sure you have RW permissions for your file? If everything checks out like you say then as pm100 said, it must be UB elsewhere in your program. – yano Mar 02 '17 at 00:29

2 Answers2

0

A seg fault with the fseek function in C is almost always caused by a failed fopen. Ensure that the file you are trying to open actually exists. If by some chance you are relying on the r+ parameter to create a new file for you then you are mistaken, r+ does not create a new file in C. Try verifying the return value of fopen whenever possible. Just in case you weren't aware rewind is simply an fseek that goes to the start of the file so that's why you are likely getting a problem there too. Hope this helps!

Happy coding!

TheCrifix
  • 79
  • 1
  • 3
  • 11
  • Ok. I checked all of the functions individually, as stated in the comments. I know pass.key exists, I made it exist. I have verified absolutely that all of the other lines work. – picklesrevil Mar 02 '17 at 00:09
0

This is more like an extended comment than a proper answer. Here's how your code must look like:

FILE *passkey;
if (!(passkey = fopen("pass.key", "r+")) {
    perror("pass.key");
    exit(EXIT_FAILURE);
}
if(-1 == fseek(passkey, 0, SEEK_END)) {
    perror("pass.key");
    exit(EXIT_FAILURE);
}
if(-1 == (filesize = ftell(passkey)) {
    perror("pass.key");
    exit(EXIT_FAILURE);
}
rewind(passkey);

It is very likely that this code does not get to the last line. But if it does, then the problem must be somewhere else. Then I would recommend running your program with a memory debugger (such as valgrind or Electric Fence).

DYZ
  • 55,249
  • 10
  • 64
  • 93