Thread: help please

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    178

    help please

    Okay, i am really having trouble understanding how to save/load a file. I am pretty new to c++ not a complete newbie but still a newbie. Anyways my save function works fine but i can't get the load funtion to work. this is what i have for the save function

    void save()
    {
    ofstream fout; //used to open file for writing

    fout.open("sav.dat");
    fout<<"stat.class name = "<<stat.classname<<"\nstat.hp ="<<stat.hp<<"\n";
    fout<<"stat.maxhp = "<<stat.maxhp<<"\nstat.mp = "<<stat.mp<<"\n";
    fout<<"stat.maxmp = "<<stat.maxmp<<"\nstat.attack = "<<stat.attack<<"\n";
    fout<<"stat.defense = "<<stat.defense<<"\nstat.bons = "<<stat.bons<<"\n";
    fout<<"stat.exp = "<<stat.exp<<"\nstat.level = "<<stat.level<<"\n";
    fout.close();
    cout<<"\n\n\n File Saved.\n\n";
    system("pause");
    menu();
    } //end save

    this works dandy, it's loading that's the problem, here is what i have

    void load()
    {
    int data;
    ifstream fin; //used for reading

    fin.open("sav.dat");
    while(!fin.eof())
    fin>>data;
    fin.close();

    cout<<"\n\n File Loaded.\n\n";
    system("pause");
    menu();
    } //end load

    when i call the load function, it will just lag and never print "File loaded" to the screen. Does anyone have any suggestions, and i don't really understand pointers very good, is this possible w/out using them. Thanks.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    your problem is here!

    while(!fin.eof())
    fin>>data;

    my estimation and i could be wrong is this,

    fin>>data looks for a text mode int if it cant find on then it leaves the file pointer where it is and the file will never hit EOF!

    btw you will only read one int with that loop

    solution use

    fin.read((char*)&data, sizeof(int)); // read a single int "data"

    to read the int

    and open with ios::binary or ios_base::binary
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    178
    Allright. Now it doesn't lock up anymore, and it will display the file loaded. But now i'm stuck on one last thing(at least I hope) How do i assign the data that it reads to a variable?

  4. #4
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb Try something like this(maybe)

    Well, since you are loading 10 variables from the file, you can read them in one at a time:

    int stats[10];

    ifstream fin;

    fin.open("sav.dat");
    for(int i=0; i < 10; i++)
    {
    fin.read(char*)&stats[i], sizeof(int));
    }
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    178
    Okay i think, i've got it figured out, I just don't know how to fix it. With this latest way i've been told with the for loop, it still half works, but would not assign the variables to my structure. I dorked around with it for about 3 hours and still couldn't figure it out. So i added a cout statement to the for loop outputting what it is reading from the file. It is just outputting the hex memory address x amount of times. Does anyone know how to fix this. (throws palms to his face and breaks out in tears), i've been working on this for a week now and it just won't work. I think it hates me (: Thanks for all the help.

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    insted of "int" in sizeof(int) use sizeof(stat) or whatever is the name of the reference variable.


    here is some sample code... [not tested]

    Code:
    struct statistics
    {
     char name[15];
     int hp;
     int h;
    }stat;
    
    void save() 
    { 
    fstream fout; //used to open file for writing 
    
    cout<<"ENTER NAME : ";
    cin.getline(stat.name,15,'/n');
    cout<<"ENTER HIT POINTS : ";
    cin>>stat.hp;
    cout<<"ENTER HEALTH : ":
    cin>>stat.h;
    
    fout.open("sav.dat",ios::app); 
    fout.write((char*)&stat,sizeof(stat));
    fout.close(); 
    
    cout<<"\n\n\n File Saved.\n\n"; 
    system("pause"); 
    menu(); 
    } //end save 
    
    void load() 
    { 
    fstream fin; //used for reading 
    
    cout<<"SAVED DATA OF PLAYERS\n\n";
    
    fin.open("sav.dat",ios::in); 
    fin.read((char*)&stat,sizeof(stat)); 
    while(!fin.eof()) 
    {
     cout<<"NAME : "<<stat.name<<"\n";
     cout<<"HIT POINTS : "<<stat.hp<<"\n";
     cout<<"HEALTH : "<<stat.h<<"\n\n\n";
     fin.read((char*)&stat,sizeof(stat)); 
    }
    fin.close(); 
    
    cout<<"\n\n File Loaded.\n\n"; 
    system("pause"); 
    menu(); 
    } //end load
    -

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    178
    almost got it, only one last thing, it loads the file, but it loads it the the first variable in my structure. for example, i have a stats function thats used to display your characters stats to the screen. Here is what it outputs:

    the level warrior
    30
    30
    5
    5
    20
    15
    200
    0
    0

    exp 0
    hp 0/0
    hp 0/0
    etc

Popular pages Recent additions subscribe to a feed