Save function not working

This is a discussion on Save function not working within the C++ Programming forums, part of the General Programming Boards category; Is this legal becuase it does not work, the file is created according to the argument passed into the function ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    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();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    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.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    i got it, nice ta

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  5. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM

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