Back and having a problem with loading variables from a file

This is a discussion on Back and having a problem with loading variables from a file within the C++ Programming forums, part of the General Programming Boards category; CODE: Code: #include <iostream> #include <stdlib.h> #include <wincon.h> #include <conio.h> #include <windows.h> #include <fstream> #include "gkinfo.h" using namespace std; ofstream ...

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    Back and having a problem with loading variables from a file

    CODE:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <wincon.h>
    #include <conio.h>
    #include <windows.h>
    #include <fstream>
    #include "gkinfo.h"
    using namespace std;
    ofstream save;
    ifstream load;
    void savestats(student &x, course *c)
    {
        clrscr();
        save.open("stats.dat");
        cout<<"Saving statistics...";
        int i;
        /*save the class*/
        //save << &x << endl;
        /*Save variables*/
        save << x.FNAME<<endl;    //Firstname
        save << x.LNAME<<endl;    //Lastname
        save << x.ID<<endl;       //Student ID
        /*Course variable save*/
        for(i=1; i<25; i++)
        {
            //save << &c[i] << endl;
            save << c[i].cGrade<<endl;        //Grade
            save << c[i].CourseName<<endl;    //Course Name
            save << c[i].nCurrentPoints<<endl; //Current poits in class
            save << c[i].nMaxPoints<<endl;     //Max points you got
            save << c[i].nHour<<endl;         //Hour of class
        }
        save.close();   //close the file
        cout<<"Stats saved!";
    }
    
    void loadstats(student &x, course *c)
    {
        clrscr();
        load.open("stats.dat");
        cout<<"Loading statistics..."<<endl;
        int j;
        /* load vars*/
        //load >> &x;
        load>>x.FNAME;  //First Name
        load>>x.LNAME;  //LastName
        load>>x.ID;     //ID
        for (j=25; j>1; j--)
        {
            //load >> &c[j];
            load >> c[j].cGrade;
            load >> c[j].CourseName;
            load >> c[j].nCurrentPoints;
            load >> c[j].nMaxPoints;
            load >> c[j].nHour;
        }
        load.close();
        cout<<x.FNAME<<"'s"<< "Stats opened!";
        cin.get();
    }
    PROBLEM:
    The problem is that the file doesn't want to load any variable other than "x.LNAME". It will load that, but none of the other variables. I have NO idea why it is loadin that one, but if somebody can help I would appreciate it
    EDIT: I should note that the variables are being saved to the file (checked using a hex editor).
    To code is divine

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I can't tell why the code isn't working as you want. I see nothing that isn't legal. I do have some suggestions that may or may not help.

    First, the default behaviour of ofstreams is ios::trunc, which means that everytime you open a given file it will be overwritten. If you try to save more than one student object this may cause a problem as it appears that you will open the same file each time you write a new students data and close it after writing a given students data to file.

    Second, ofstreams will default to output using text format irrespective of the extension type listed, rather than binary format and therefore the output should be visible in a routine text editor unless you use the ios::binary option when opening the stream/file which will save the output in binary format, in which case you probably will need a hex editor to "read" it in the file.


    Third, it is a good idea to check to be sure the file is open before using it.

    Fourth, debugging is an important task to learn. I like to sprinkle my code with output statements to see where things go awry. Others like to use a debugger, or a combination of the two techniques. For example, I'd do something like this to see whats being read in.

    load>>x.FNAME; //First Name
    cout << x.FNAME << endl;
    load>>x.LNAME; //LastName
    cout << x.LNAME << endl;
    load>>x.ID; //ID
    cout << x.ID << x.ID << endl;

    Fifth, if that doesn't help arrive at an answer, I'd request you post the declaration of the student class and the driver portion of the program where you actually call the functions you have declared/defined/posted.

    Sixth, reading arrays using indexes starting with 1 is always frought with potential for errors, so be careful. Posted code will help tell if this is a real concern or not.
    Last edited by elad; 03-05-2005 at 01:48 PM.
    You're only born perfect.

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Well I just got done adding some debug information, and the weirdest thing happened.
    I called a "cout" after every variable being loaded, and it turned out it was being loaded (by the variable being printed). However, for some reason, after that point the variables aren't being recognized other than the x.LNAME.


    EDIT: Code posted in a few minutes
    Last edited by 7smurfs; 03-05-2005 at 02:02 PM.
    To code is divine

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Ok, here is the code:

    Prototype:
    Code:
    void loadstats(course *c, student &x);
    void savestats(course *c, student &x);
    Decleration:
    Code:
    class course
    {
        public:
            int nHour;
            char cGrade;
            double nMaxPoints;
            double nCurrentPoints;
            char CourseName[255];
            
    };
    class student
    {
        public:
            int ID;
            char FNAME[25];
            char LNAME[25];
    };
    Load function being called:
    Code:
            else if(MainMenuChoice==2)
            {
                clrscr();
                loadstats(c, x);
                menu(x, c);
            }
    Save function being called:
    Code:
           if(choice==4)
           {
                savestats(c, x);
            }
    If you need the entire source tell me so and I can zip the files and post them here.
    To code is divine

  5. #5
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Well, it seems like it was somehow a problem with the loop in the load function causing the problem o-O

    I reversed the loop and it works now; except for the class "course".


    EDIT: FINALLY! I've been working on this off-and-on since October, and now it's done! Thanks for the constant help guys!
    Last edited by 7smurfs; 03-05-2005 at 10:14 PM.
    To code is divine

  6. #6
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    congratz smurf:)

    elad you said trunc overwrittens every thing in a file , but doesn;t it just deleates it : the turtourial says :

    ios::app -- Append to the file
    ios::ate -- Set the current position to the end
    ios::trunc -- Delete everything in the file
    PLay MystWind beta , within two years

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    MystWind,

    'overwritten' with nothing is the same as 'deleted' isn't it? Technically, I think ios::trunc means truncate the file length to 0.

    Usually, when you open a file you are going to write to it, so it can be thought of as 'overwriting' the entire file. But, a simple test will show that if you ios::trunc, and don't write anything to the file, the file will be 'deleted'.
    Last edited by 7stud; 03-06-2005 at 02:47 PM.

Popular pages Recent additions subscribe to a feed

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