0
votes

I have a truly strange problem on my hand. I am reading a file and run a fread, and instead of skipping ahead by the 6 bytes like it should, it goes backwards 4092-6 bytes!

Here is the call:

int bread = sizeof(RoughnessReadPoint) * fread(&roughnessBuffer, sizeof(RoughnessReadPoint), 1, ptr);

Here is the associated struct:

struct RoughnessReadPoint
{
   S8 x;
   S8 y;
   S8 z;
   S8 x2;
   S8 y2;
   S8 z2;
};

Here is a GDB trace showing the read:

216         int bread = sizeof(RoughnessReadPoint) * fread(&roughnessBuffer, sizeof(RoughnessReadPoint), 1, ptr);
(gdb) p ptr->_IO_read_ptr
$5 = 0x5555557cce42 "\002\017\374\016\002\373˱N\001\002\017\374\017\020\020"
(gdb) n
217             totalBytesRead += bread;
(gdb) p ptr->_IO_read_ptr
$6 = 0x5555557cce48 "˱N\001\002\017\374\017\020\020"

As you can see it jumps by 6 as its supposed to.

Now the problem section:

216         int bread = sizeof(RoughnessReadPoint) * fread(&roughnessBuffer, sizeof(RoughnessReadPoint), 1, ptr);
(gdb) p ptr->_IO_read_ptr
$14 = 0x5555557cce4c "\002\017\374\017\020\020"
(gdb) n
217             totalBytesRead += bread;
(gdb) p ptr->_IO_read_ptr
$15 = 0x5555557cbe52 "\261N\001\002\017\374\017\002\374ͱN\001\002\017\373\017\002\374бN\001\002\017\374\017\002\373ұN\001\002\017\373\016\002\373ձN\001\002\017\373\016\002\373ױN\001\002\017\374\016\002\373ڱN\001\002\017\373\016\002\373ܱN\001\002\017\373\017\002\373߱N\001\002\017\373\017\002\373\342\261N\001\002\017\373\017\002\374\344\261N\001\002\017\373\017\002\373\347\261N\001\002\017\373\017\002\373\351\261N\001\002\017\373\017\002\373\354\261N\001\002\017\373\017\002\373\356\261N\001\002\017\374\016\002\373\361\261N\001\002\017\373\016\002\373\363\261N\001\002\017\374\016\002\373\366\261N\001\002\017\374\016\002\373\370\261N\001\002\017\374\016\002\373\373\261N\001\002\017\374\016\002\373", <incomplete sequence \375>...

Notice, the read pointer went BACKWARDS 4086 bytes.

For good measure, I printed out how many bytes were read here as well...

(gdb) p bread
$16 = 6

Does anyone have a clue what is going on?

Other information: This is obviously a debug build, but it happens when compiling with -O2 as well This is ubuntu 18.04 compiled with g++ The file is about 130MB long and this happens at about 83MB into the file

If I keep reading, the file gets close to the same point over and over again only to fall back 4086 bytes every time!

1
The fields of a FILE aren't mean to be read by users of the library. Instead, call ftell to get the current file offset and use it to verify that you're at the position you want to be at.dbush
You are assuming that the _IO_read_ptr is tracking a contiguous block of virtual memory. That may not be so. That's internal implementation and unless you have gone into the source code of fread to see what it is really doing you should not make such assumptions.kaylum
You are correct. Using ftell does produce the correct (expected) file offset. Thanks, I found a problem with the file I was reading. Oddly enough it just happens to occur at the same byte read the _IO_read_ptr jumps.wfb0002

1 Answers

1
votes

As dbush and kaylum said in the comments, I should have used ftell as opposed to using the internal variables in the FILE struct. My issue was with the file I was reading - it just happened to occur at the same point the internal variable jumped.