Reading from disk error
I am wondering what kind of things would cause fread() to return 0, for 0 items read from disk. After I have written to disk, and then try to read it in, with fread(), it returns 0, and I''m wondering why this is happening. Help is appreciated. Thanks.
Greg FieldsSyntasoft Gameswww.syntasoft.comPost-Time Horse Racing (Available Now)
This could happen because The FILE pointer is null.
If the FILE pointer is null, and you try to fread, it will return a 0. The FILE pointer could be null because the file doesn''t exist. When you want to read from a file, specify "r" in the fopen function.
Hope that helps.
If the FILE pointer is null, and you try to fread, it will return a 0. The FILE pointer could be null because the file doesn''t exist. When you want to read from a file, specify "r" in the fopen function.
Hope that helps.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Or maybe you''re trying to fread() from a text file? I don''t know if fread() works on text files, but it is possible that that''s what''s causing the trouble.
/. Muzzafarath
Mad House Software
/. Muzzafarath
Mad House Software
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
I don''t think that''s it. I can use fread with a text file. No problem.But then again, that maybe be different from compiler to compiler?
I still think that you didn''t check the FILE pointer before using it. Anyway, post and tell us if our advice didn''t help you .
I still think that you didn''t check the FILE pointer before using it. Anyway, post and tell us if our advice didn''t help you .
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Well... I don''t think the FILE pointer is NULL, I could be wrong? Anyways, here is the code take a look. The load and save functions are here:
void Load(void)
{
strcpy(string2,loadedPlaybook);
strcpy(loadedPlaybook," ");
lpdd->FlipToGDISurface(); // dialog box gets name of playbook to load
DialogBox (hinst, TEXT ("PLAYBOOK_LOAD_DIALOG"), main_window_handle, &DlgProc);
if(strcmp(loadedPlaybook,"")){
// ensures the playbook goes in the default ''playbooks'' directory
sprintf(string1,"playbooks//%s.pbk",loadedPlaybook);
if(strcmp(loadedPlaybook," ")){
if(file_playbook = fopen(string1,"r")){ // opens the file specified by the user
fread(&playbook,sizeof(playbook),1,file_playbook);
fclose(file_playbook);
lpdd->FlipToGDISurface();
Message("Success","Playbook loaded successfully.");
EnableMenuItem(PLAYBOOK_menu,PLAYBOOK_SAVE,MF_ENABLED);
PLAYBOOK_LOADED = TRUE;
for(p=0;p<11;p++){
tempVar = p;
playbook.play[OFFENSIVE_PLAY_SELECTED].OPlayer[tempVar].sprite.x = LOS-playbook.play[OFFENSIVE_PLAY_SELECTED].OPlayer[tempVar].distanceFromLOS;
playbook.play[DEFENSIVE_PLAY_SELECTED].DPlayer[tempVar].sprite.x = LOS+playbook.play[DEFENSIVE_PLAY_SELECTED].DPlayer[tempVar].distanceFromLOS;
}
}
else{
Message("Error","Playbook doesn''t exist.");
strcpy(loadedPlaybook,string2);
}
} else strcpy(loadedPlaybook,string2);
}
else Message("Error","No playbook has been entered.");
}
void Save(void)
{
// makes loadedPlaybook = " "
strcpy(string2,loadedPlaybook);
strcpy(loadedPlaybook," ");
for(p=0;p<11;p++){ // for correct loading purposes
tempVar = p;
playbook.play[OFFENSIVE_PLAY_SELECTED].OPlayer[tempVar].distanceFromLOS = LOS - (int)playbook.play[OFFENSIVE_PLAY_SELECTED].OPlayer[tempVar].sprite.x;
playbook.play[DEFENSIVE_PLAY_SELECTED].DPlayer[tempVar].distanceFromLOS = (int)playbook.play[DEFENSIVE_PLAY_SELECTED].DPlayer[tempVar].sprite.x - LOS;
}
lpdd->FlipToGDISurface(); // dialog box gets name of playbook to save
DialogBox (hinst, TEXT ("PLAYBOOK_SAVE_DIALOG"), main_window_handle, &DlgProc);
if(strcmp(loadedPlaybook,"")){ // if loadedPlaybook is not blank
sprintf(string1,"playbooks//%s.pbk",loadedPlaybook);
if(file_playbook = fopen(string1,"r")){ // opens the file specified by the user
fclose(file_playbook); // ask if user wants to overwrite existing file
if(strcmp(loadedPlaybook," ")){
if(MessageBox(main_window_handle,"Playbook already exists, overwrite?","Playbook exists",MB_YESNO)==IDYES){
if(file_playbook = fopen(string1,"w")){ // create file
fwrite(&playbook,sizeof(playbook),1,file_playbook);
fclose(file_playbook);
Message("Playbook overwritten","Old playbook has been overwritten successfully with the new playbook.");
}
else Message("Error","Unable to save playbook.");
}
} else strcpy(loadedPlaybook,string2);
}
else{
if(file_playbook = fopen(string1,"w")){
if(strcmp(loadedPlaybook," ")){
fwrite(&playbook,sizeof(playbook),1,file_playbook);
fclose(file_playbook);
Message("Playbook saved","Playbook saved successfully.");
} else strcpy(loadedPlaybook,string2);
}
else Message("Error","Unable to save playbook.");
}
}
else Message("Error","No playbook has been entered.");
}
Sorry for the long, sloppy code... and how do you make the code smaller than your regular text? Thanks for your time.
void Load(void)
{
strcpy(string2,loadedPlaybook);
strcpy(loadedPlaybook," ");
lpdd->FlipToGDISurface(); // dialog box gets name of playbook to load
DialogBox (hinst, TEXT ("PLAYBOOK_LOAD_DIALOG"), main_window_handle, &DlgProc);
if(strcmp(loadedPlaybook,"")){
// ensures the playbook goes in the default ''playbooks'' directory
sprintf(string1,"playbooks//%s.pbk",loadedPlaybook);
if(strcmp(loadedPlaybook," ")){
if(file_playbook = fopen(string1,"r")){ // opens the file specified by the user
fread(&playbook,sizeof(playbook),1,file_playbook);
fclose(file_playbook);
lpdd->FlipToGDISurface();
Message("Success","Playbook loaded successfully.");
EnableMenuItem(PLAYBOOK_menu,PLAYBOOK_SAVE,MF_ENABLED);
PLAYBOOK_LOADED = TRUE;
for(p=0;p<11;p++){
tempVar = p;
playbook.play[OFFENSIVE_PLAY_SELECTED].OPlayer[tempVar].sprite.x = LOS-playbook.play[OFFENSIVE_PLAY_SELECTED].OPlayer[tempVar].distanceFromLOS;
playbook.play[DEFENSIVE_PLAY_SELECTED].DPlayer[tempVar].sprite.x = LOS+playbook.play[DEFENSIVE_PLAY_SELECTED].DPlayer[tempVar].distanceFromLOS;
}
}
else{
Message("Error","Playbook doesn''t exist.");
strcpy(loadedPlaybook,string2);
}
} else strcpy(loadedPlaybook,string2);
}
else Message("Error","No playbook has been entered.");
}
void Save(void)
{
// makes loadedPlaybook = " "
strcpy(string2,loadedPlaybook);
strcpy(loadedPlaybook," ");
for(p=0;p<11;p++){ // for correct loading purposes
tempVar = p;
playbook.play[OFFENSIVE_PLAY_SELECTED].OPlayer[tempVar].distanceFromLOS = LOS - (int)playbook.play[OFFENSIVE_PLAY_SELECTED].OPlayer[tempVar].sprite.x;
playbook.play[DEFENSIVE_PLAY_SELECTED].DPlayer[tempVar].distanceFromLOS = (int)playbook.play[DEFENSIVE_PLAY_SELECTED].DPlayer[tempVar].sprite.x - LOS;
}
lpdd->FlipToGDISurface(); // dialog box gets name of playbook to save
DialogBox (hinst, TEXT ("PLAYBOOK_SAVE_DIALOG"), main_window_handle, &DlgProc);
if(strcmp(loadedPlaybook,"")){ // if loadedPlaybook is not blank
sprintf(string1,"playbooks//%s.pbk",loadedPlaybook);
if(file_playbook = fopen(string1,"r")){ // opens the file specified by the user
fclose(file_playbook); // ask if user wants to overwrite existing file
if(strcmp(loadedPlaybook," ")){
if(MessageBox(main_window_handle,"Playbook already exists, overwrite?","Playbook exists",MB_YESNO)==IDYES){
if(file_playbook = fopen(string1,"w")){ // create file
fwrite(&playbook,sizeof(playbook),1,file_playbook);
fclose(file_playbook);
Message("Playbook overwritten","Old playbook has been overwritten successfully with the new playbook.");
}
else Message("Error","Unable to save playbook.");
}
} else strcpy(loadedPlaybook,string2);
}
else{
if(file_playbook = fopen(string1,"w")){
if(strcmp(loadedPlaybook," ")){
fwrite(&playbook,sizeof(playbook),1,file_playbook);
fclose(file_playbook);
Message("Playbook saved","Playbook saved successfully.");
} else strcpy(loadedPlaybook,string2);
}
else Message("Error","Unable to save playbook.");
}
}
else Message("Error","No playbook has been entered.");
}
Sorry for the long, sloppy code... and how do you make the code smaller than your regular text? Thanks for your time.
Greg FieldsSyntasoft Gameswww.syntasoft.comPost-Time Horse Racing (Available Now)
>> how do you make the code smaller than your regular text? <<
Put all of your code between < code> < /code> tags (without the spaces). Or you could put it in [ source] [ /source] tags.
/. Muzzafarath
Mad House Software
Edited by - Muzzafarath on June 11, 2000 11:24:10 AM
Put all of your code between < code> < /code> tags (without the spaces). Or you could put it in [ source] [ /source] tags.
This is inside the code tag.
This is inside the source tag. int void double
/. Muzzafarath
Mad House Software
Edited by - Muzzafarath on June 11, 2000 11:24:10 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
what is the "playbook" variable? a struct? maybe change this line to:
(& then change the write the same way..)
this is so the sizeof() references the playbook''s type and not the variable itself..
just a possible problem I have had before. sometimes the compiler I have doesn''t like references to specific variables caus'' It can figure the size? (I assume)
http://www.ill-lusion.com
fread(&playbook,sizeof(PLAYBOOK_TYPE),filepointer);
(& then change the write the same way..)
this is so the sizeof() references the playbook''s type and not the variable itself..
just a possible problem I have had before. sometimes the compiler I have doesn''t like references to specific variables caus'' It can figure the size? (I assume)
http://www.ill-lusion.com
laxdigital.com
[email=ziggy@laxdigital.com]ziggy@laxdigital.com[/email]
[email=ziggy@laxdigital.com]ziggy@laxdigital.com[/email]
The weird thing about this is that it works sometimes. When I changed from sizeof(variable) to sizeof(variable_typ), it worked 2 times, and then didn''t work again! What is going on here?
Greg FieldsSyntasoft Gameswww.syntasoft.comPost-Time Horse Racing (Available Now)
that is not as strange as it sound (to me anyway...)
how do you create/destroy/handle the "playbook"?
you should probably "new" / "delete" it every time you load it. If it is not being freed/renewed properly this maybe your problem... don''t know what else to try without the rest of the source to examine...
Ziggy,
http://www.ill-lusion.com
how do you create/destroy/handle the "playbook"?
you should probably "new" / "delete" it every time you load it. If it is not being freed/renewed properly this maybe your problem... don''t know what else to try without the rest of the source to examine...
Ziggy,
http://www.ill-lusion.com
laxdigital.com
[email=ziggy@laxdigital.com]ziggy@laxdigital.com[/email]
[email=ziggy@laxdigital.com]ziggy@laxdigital.com[/email]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement