read in file offset

S

spleen

Hiya,

Im working on a project to read in a file (its a bitmap, but thats
irrelevant) and I want to read in all the data after an offset, ive
tried using -

fread(bitmap_buffer, offset, 1, fp);

but this doesnt work and only reads in the value at the offset (0x436h
irrelevant once again i suppose).

so my question is how do i read in all the data AFTER a certain
offset, into my buffer?

any help much appreciated, and no this isnt school work (school terms
out anyway) Im just doing this becuase im learning to program and
starting to enjoy writing programs that can do useful stuff :)

thanks
 
M

Malcolm

spleen said:
Im working on a project to read in a file (its a bitmap, but thats
irrelevant) and I want to read in all the data after an offset, ive
tried using -

fread(bitmap_buffer, offset, 1, fp);

but this doesnt work and only reads in the value at the offset (0x436h
irrelevant once again i suppose).

so my question is how do i read in all the data AFTER a certain
offset, into my buffer?
The offset for the data (presumably the pixels of the image) is given
somewhere in the header.
That header will probably also contain the image dimensions, and information
that tells you how many bits are stored per pixel.

If you have this information,
1) fseek() to the file position where the pixel data starts.
2) allocate memory with malloc() to hold the pixels
3) fread the whole lot in with one go.

If you don't have the information (eg if the data is compressed)
1) fseek() to the file positon where pixel data starts.
2) call fgetc() to read the data one byte at a time.
3) decode the data, expand your buffer if needed.
4) check for EOF which tells you when the data has finished.
 
J

Jens.Toerring

spleen said:
Im working on a project to read in a file (its a bitmap, but thats
irrelevant) and I want to read in all the data after an offset, ive
tried using -
fread(bitmap_buffer, offset, 1, fp);
but this doesnt work and only reads in the value at the offset (0x436h
irrelevant once again i suppose).

This would read 'offset' bytes at the current position in the file.
You can use the fseek() fucntion to move within the file, i.e. use

fseek( fp, offset, SEEK_SET );

to jump to position 'offset' in the file and then start reading.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top