Advertisement

Writing files to disk problem

Started by June 08, 2000 06:05 PM
2 comments, last by Gf11speed 24 years, 6 months ago
I am able to write to disk ok, however, when I attempt to read that data which I wrote to disk, my program just bombs and returns to Windows. The fread() function returns 0, for 0 items read. Why is it doing this? I checked the return value for fwrite and fread, and fread is the only one returning zero. I just don''t know what is causing this. Her is the code to write and read the data... any help is appreciated. Two functions: 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 that long code... 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)
I''m a little confused by your strcmp logic. You do know that strcmp returns 0 if there''s an exact match, correct?

Anyway, just put a breakpoint at the start of the function and step through it one line at a time, making sure all the variables have the values you''d expect. I''d pay particular attention to how the fopen and fread lines behave, according to your description of the problem.
Advertisement
Gf11speed, you really should try to add some spaces to your code. Especially between arguments to functions

/. Muzzafarath
Mad House Software

Edited by - Muzzafarath on June 9, 2000 2:35:40 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
I''m sorry about the code. It looks much neater in the compiler.

The problem I''m having is with the fread() function. It returns 0, meaning it read 0 items from disk... this is called after I have already saved to disk successfully.
Greg FieldsSyntasoft Gameswww.syntasoft.comPost-Time Horse Racing (Available Now)

This topic is closed to new replies.

Advertisement