Binary File Access
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...
------------------------------------------------------------I wrote the best video game ever, then I woke up...
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);
Example:
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
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);
Example:
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
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);
return(buf);
--------------------
information is the illusion of knowledge
----------------------
//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);
return(buf);
--------------------
information is the illusion of knowledge
"It's obvious isn't it Miller? Curiosity killed these cats!"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement