C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-19-2009, 08:24 AM   #1
Registered User
 
rogster001's Avatar
 
Join Date: Aug 2006
Location: Liverpool UK
Posts: 452
Save function not working

Is this legal becuase it does not work, the file is created according to the argument passed into the function ok but after closing it is blank, if i do it without the if statements it works fine.

Code:
void SolBoard::SaveGame(int myfile)    
{
    ofstream Gamefile;

    if(myfile == 0)                                 //which file to write over.
    ofstream Gamefile("mysave0.txt");
    if(myfile == 1)
    ofstream Gamefile("mysave1.txt");
    if(myfile == 2)
    ofstream Gamefile("mysave2.txt");

    for(countclick = 0; countclick < 49; countclick++)
    {
        Gamefile << moves_record[moves][countclick];
        Gamefile<< " ";
    }
    Gamefile.close();
}
rogster001 is offline   Reply With Quote
Old 11-19-2009, 08:26 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,341
You should be using the open() member function instead of creating a new ofstream object in a scope local to the respective if statements. You should also consider what happens if myfile contains none of the values listed.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-19-2009, 08:36 AM   #3
Registered User
 
rogster001's Avatar
 
Join Date: Aug 2006
Location: Liverpool UK
Posts: 452
ok thanks i will look up the function, i know i should put the error checking in too, file i/o does my swede in.
rogster001 is offline   Reply With Quote
Old 11-19-2009, 09:27 AM   #4
Registered User
 
rogster001's Avatar
 
Join Date: Aug 2006
Location: Liverpool UK
Posts: 452
i got it, nice ta
rogster001 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an error with OpenGL: collect2: ld returned 1 exit status Lorgon Jortle C++ Programming 6 05-08-2009 08:18 PM
Which Library Files for these unreferenced functions? lehe C++ Programming 3 01-31-2009 10:30 PM
Compiling sample DarkGDK Program Phyxashun Game Programming 6 01-27-2009 03:07 AM
Seg Fault in Compare Function tytelizgal C Programming 1 10-25-2008 03:06 PM
Troubleshooting Input Function SiliconHobo C Programming 14 12-05-2007 07:18 AM


All times are GMT -6. The time now is 02:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22