fseek() - generic question dued to a problem
-
I have a save-file from my program so structured:
char id[32]; // "DATASET_1"
int blocksize;
:
: <- various data for an amount of 'blocksize' bytes
:char id[32]; // "DATASET_2"
int blocksize;
:
: <- various data for an amount of 'blocksize' bytes
:char id[32]; // "DATASET_3"
int blocksize;
:
: <- various data for an amount of 'blocksize' bytes
:With a generic number of "DATASET_#" When I try to load the file, I use code similiar to this:
char id[32];
int blocksize;
FILE* f;f = fopen(filename, "rb");
while (!feof(f))
{
fread(id,sizeof(char),32,f); // read id
fread(&blocksize,sizeof(int),1,f); // read blocksizeif (strcmp(id,"DATASET\_1") == 0) { fread( /\* appropriate reading here \*/); } else if (strcmp(id,"DATASET\_2") == 0) { fread( /\* appropriate reading here \*/); } else // DATASET unknown { // skip unknow block of data fseek(f,blocksize,SEEK\_CUR); }
}//while
fclose(f);
When I do this, the while loop never exits if, for example, the last dataset is unknown, it seems that the fseek() function has no effect on the next "while (!feof(f))" call. I must take care of something about fseek under NT or WinXP or I must look for an error elsewhere ? By now, I assume the data to be written well. Thanks in advance for any suggestion.
-
I have a save-file from my program so structured:
char id[32]; // "DATASET_1"
int blocksize;
:
: <- various data for an amount of 'blocksize' bytes
:char id[32]; // "DATASET_2"
int blocksize;
:
: <- various data for an amount of 'blocksize' bytes
:char id[32]; // "DATASET_3"
int blocksize;
:
: <- various data for an amount of 'blocksize' bytes
:With a generic number of "DATASET_#" When I try to load the file, I use code similiar to this:
char id[32];
int blocksize;
FILE* f;f = fopen(filename, "rb");
while (!feof(f))
{
fread(id,sizeof(char),32,f); // read id
fread(&blocksize,sizeof(int),1,f); // read blocksizeif (strcmp(id,"DATASET\_1") == 0) { fread( /\* appropriate reading here \*/); } else if (strcmp(id,"DATASET\_2") == 0) { fread( /\* appropriate reading here \*/); } else // DATASET unknown { // skip unknow block of data fseek(f,blocksize,SEEK\_CUR); }
}//while
fclose(f);
When I do this, the while loop never exits if, for example, the last dataset is unknown, it seems that the fseek() function has no effect on the next "while (!feof(f))" call. I must take care of something about fseek under NT or WinXP or I must look for an error elsewhere ? By now, I assume the data to be written well. Thanks in advance for any suggestion.
from the MSDN for fseek: " You can use fseek to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream. " fseek doesn't care about EOF. -c
"What the hell are you getting so upset about? I thought you didn't believe in God". "I don't," she sobbed, bursting violently into tears, "but the God I don't believe in is a good God, a just God, a merciful God. He's not the mean and stupid God you make Him out to be". -- Joseph Heller Smaller Animals Software, Inc.
-
from the MSDN for fseek: " You can use fseek to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream. " fseek doesn't care about EOF. -c
"What the hell are you getting so upset about? I thought you didn't believe in God". "I don't," she sobbed, bursting violently into tears, "but the God I don't believe in is a good God, a just God, a merciful God. He's not the mean and stupid God you make Him out to be". -- Joseph Heller Smaller Animals Software, Inc.
Ok, but does feof() take care of a previous fseek() ? This loop never exits in a test program I've done... try it out plz if you can:
FILE* f;
long i;f = fopen("c:\\a.txt","rb");
i = 0;
while (!feof(f))
{
fseek(f,i++,SEEK_SET); // seeks at 0,1,2,3,... until reach EOF (?)
}fclose(f);
The file "a.txt" contains three simple characters.
-
Ok, but does feof() take care of a previous fseek() ? This loop never exits in a test program I've done... try it out plz if you can:
FILE* f;
long i;f = fopen("c:\\a.txt","rb");
i = 0;
while (!feof(f))
{
fseek(f,i++,SEEK_SET); // seeks at 0,1,2,3,... until reach EOF (?)
}fclose(f);
The file "a.txt" contains three simple characters.
Is your program C ( not C++) ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
-
Is your program C ( not C++) ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
-
It's C++, MFC app. By now I have solved with something like this:
while (fread(id,sizeof(char),32,f) != 0)
{
// almost same stuff here
}But I thought that it had should exit if fseek() seeks the position over the EOF marker...
OK - I am curious, is there a reason you're using C functions ? What advantage do you get from fread, as opposed to C++ equivelants, souch as ifstream ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
-
OK - I am curious, is there a reason you're using C functions ? What advantage do you get from fread, as opposed to C++ equivelants, souch as ifstream ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
-
Only because I'm in the abit of using them, so I can reuse some old code :) Do you suggest me using ifstream instead ?
I would always recommend using C++ code over C equivelants, the C++ libraries were written not just to do the same as the C stuff, but to do it better. I was seriously asking, because I notice a lot of people posting C code and I'm happy to learn that it does some things better, better to ask and find out than wade in with my opinion and be told ;-) Your code looked to me like it would be a lot more readable using ifstream though, although I admit I only glanced over it. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
-
I would always recommend using C++ code over C equivelants, the C++ libraries were written not just to do the same as the C stuff, but to do it better. I was seriously asking, because I notice a lot of people posting C code and I'm happy to learn that it does some things better, better to ask and find out than wade in with my opinion and be told ;-) Your code looked to me like it would be a lot more readable using ifstream though, although I admit I only glanced over it. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002