Thread: .dat I/O not loading variables

  1. #1
    Join Date
    Jul 2004

    .dat I/O not loading variables

    Ok I tried putting in a load/save feature into my program, so I don't have to re-enter all the data over again. So I added the following functions:
    #include <iostream>
    #include <stdlib.h>
    #include <wincon.h>
    #include <conio.h>
    #include <windows.h>
    #include <fstream>
    #include "gkinfo.h"
    using namespace std;
    void savestats(student &x, course* c)
        ofstream save;"stats.dat");
        cout<<"Saving statistics...";"stats.dat");
        int i;
        /*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].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)
        ifstream load;"stats.dat");
        cout<<"Loading statistics...";
        int j;
        /* load vars*/
        load>>x.FNAME;  //First Name
        load>>x.LNAME;  //LastName
        load>>x.ID;     //ID
        for (j=1; j<25; j++)
        cout<<"Stats opened!";
    Now the problem is, when I have it spit out the variable x.FNAME, it spits out the name of the variable I entered after I had saved. So say I had saved it when x.FNAME==BFGH. Ok, so then after saving I'll make a new name, and set x.FNAME==HGFB. Well when I load the file, x.FNAME still equals HGFB, and not BFGH like I had it saved to. I don't understand why it is doing this. I thought that the ifstream opened up for loading the variables? Does anybody know why it is doing this?

    *NOTE: Sorry for asking so many questions *

    EDIT: I'm such an idiot, I had twice, causing the error. Let that be a lesson to everybody: Don't double call
    Last edited by 7smurfs; 12-15-2004 at 08:31 PM.
    To code is divine

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    Wow though, thats is indeed quite undefined behavior. I'll get bashed for saying this on the C++ board, but this is precisely why I use the C standard functions for file IO.
    Last edited by master5001; 12-16-2004 at 06:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  2. Creating local variables in a program?
    By fl0at in forum C Programming
    Replies: 5
    Last Post: 01-04-2008, 07:34 PM
  3. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM
  4. Saving and Loading Linked Lists With File i/o
    By Red Army in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2002, 03:19 PM
  5. Loading Screen
    By pinkcheese in forum Windows Programming
    Replies: 2
    Last Post: 04-06-2002, 11:48 PM