Thread: Loading chars from files problem

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Loading chars from files problem

    I'm having trouble loading some chars from a file... I made a code to previously save things into a file, and that works fine. I can't get the loading to work though ( like a load game function ). Here is the code :

    Code:
    type("What was your filename ?");
    cin >> filename;
    
    infile.open(filename, ios::nocreate);
    infile >> firname;
    infile >> lasname;
    infile >> sex;
    infile >> location;
    infile >> weapon;
    infile >> bplace;
    infile >> bday;
    infile >> bmonth;
    infile >> byear;
    infile >> age;
    
    type("Your name :\n");
    type(firname);
    type("\n\nyour character's last name ?\n");
    cout << lasname;
    type("\n\ncharacter male or female ?\n");
    cout << sex;
    type("\n\nWhere is your character in the game ?\n");
    cout << location;
    type("\n\nWhat is your character's primary weapon ?\n");
    cout << weapon;
    type("\n\nWhat is your character's birthplace ?\n");
    cout << bplace;
    type("\n\nWhat is your character's birthday ? [Day only]\n");
    cout << bday;
    type("\n\nWhat is your character's birth month ?\n");
    cout << bmonth;
    type("\n\nWhat is your character's birth year ?\n");
    cout << byear;
    type("\n\nTherefore what is your character's age ?\n");
    cout << age;
    Sleep(500);
    cin >> xit;
    
    return 0;
    }
    This is only part of the code. All chars and ints have been declared. TYPE is a function for a typewriter effect, so treat it as cout.

    Can anyone tell me why, when I launch this program, I get :

    "Your name is :"


    "Your age is :" etc..
    instead of

    "Your name is :"
    "NAME"

    "Your age is :"
    "AGE".

    Thanks for your help.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First, check if the opening of the file failed (infile.fail()).

    Second, are you reading the data in the same order as you saved it?

    Third, how did you declare the strings?

    Four, what's the point of these name-shortenings?

    firname;
    lasname;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    1. What do you mean by File Failed check ? Can you give me an example ?

    2. Yes, reading data in the same order as I saved it, but when I go

    cout >> "your name is" >> firname

    or something of the type I get an empty line.

    3. I declared :

    char firname[50];

    4. I was told never to put just one letter difference between characters cuz I could do typos etc...

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Korhedron
    1. What do you mean by File Failed check ? Can you give me an example ?
    Whenever you open a file for reading or writing, always check if it succeeded. This can be done using the fail() method:
    Code:
    bool ReadData()
    {
       ifstream ReadFile;
       ReadFile.open("MyFile.txt", ios::in);
    
       if(ReadFile.fail())
       {
          cout << "Unable to open file!" << endl;
          return false;
       }
    
       ...
    
       ReadFile.close();
       return true;
    }
    Remember to close it too.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    What's the BOOL ReadData thing ? And what does return true / false do ?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Korhedron
    What's the BOOL ReadData thing ? And what does return true / false do ?
    Eh? It's a function. bool is the return value (a boolean value is true or false). return means that you exit the function with the specified return value.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading files into lists
    By MiroMage in forum C Programming
    Replies: 7
    Last Post: 09-18-2008, 12:04 PM
  2. .rc files problem
    By RevengerPT in forum C Programming
    Replies: 3
    Last Post: 12-09-2005, 11:38 AM
  3. Problem with enum and files
    By Robert_Sitter in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2005, 01:40 AM
  4. I have a problem if FILEs
    By talal*c in forum C Programming
    Replies: 22
    Last Post: 01-07-2002, 05:56 AM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM