Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Binary File Access

This topic is 6626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering if anyone knew a good way to write a functions something like. char readfile(int filename, int startbyte, int endbyte)? Or some way to read varible length file sections. ------------------------------------------------------------ I wrote the best video game ever, then I woke up... Grandus.Com

Share this post

Link to post
Share on other sites
In unix/varients there''s read, and if not use fread.
Is this what you mean? It will read from the current position in the file, so you''ll have to get to the position you want to read from first, then specify the size. Using this function you can adapt it to fit your format.
Also, it loads it into an array. I don''t know where/how you wanted to store it once it''s read.

int read(int fd, void *buf, unsigned size);
size_t fread(void *buf, size_t size, size_t count, FILE *stream);


num_chars_read = read(myFile, myArray, 1000);
reads 1000 bytes and stores it in myArray...

fread(myArray, sizeof(float), 10, myFile);
reads 10 floats and stores them in myArray...

pay attention to the differences in unix vs. dos

Share this post

Link to post
Share on other sites
This is pseudo-code for windows, you may need to look up the exact syntax of the functions and of course add error handling:
//char readfile(int filename, int startbyte, int endbyte)
char *readfile(char *filename, int startbyte, int endbyte)
char buf[nn];
FILE *stream;
int result, numbytes;
int totread = (endbyte - startbytes);
// ''r+b'' sets to read and binary mode
if( (stream = fopen( filename, "r+b" )) != NULL )
//error message
// positions file to startbyte from beginning of file
result = fseek( stream, startbyte, SEEK_SET);
numbytes = fread( buf, sizeof( char ), totread, stream );
result = fclose(stream);

information is the illusion of knowledge

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!