Skip to main content
gamedev.net
🔒 Locked

Binary File Access

Started by Galileo430 2 replies 250+ views
Original Post
Galileo430
Galileo430
Well, I haveing been trying to get a straight answer out of everyone I know for the last month or so. Here is what I am trying to do. Write a functions that looks something like this,
    
unsigned short LoadShort(double beginpoint)
{
unsigned short data

// Read a short from beginpoint

// Note beginpoint is noted in number of bytes 


return data;
}
    
Does anyone know how to do this with fopen?!? Thanks. ------------------------------------------------------------ I wrote the best video game ever, then I woke up... Grandus.Com
------------------------------------------------------------I wrote the best video game ever, then I woke up...
Void
Void
Take a look at fseek. You can change the current read/write position of the file pointer with it.,
LaChoppe
LaChoppe
I highly recommend purchasing a Kernighan & Ritchie's "The C Programming Language", commonly referred to as "The Bible". It's a thin little thing, but a terrific reference for this sort of thing.

The answer you want will involve something like this, for comparison:


unsigned short LoadShort(long beginpoint, FILE *fp)
{
unsigned short data;

fseek(fp, beginpoint, SEEK_SET);
fread(&data, sizeof(unsigned short), 1, fp);

return data;
}


Get the K&R or other suitable reference, and compare this code with it to see what it does and why. Reading and dissecting other people's code is a good way to learn it yourself.

BTW: fopen the file normally with "rb" as the mode (read binary), and pass your function the file pointer. Unless you really want to reopen and close the file every time you read a short.

HTH.

-La'Choppe

Edited by - LaChoppe on September 18, 2000 3:39:16 AM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.