problems with ifstream and ofstream!! help me please!!!

This is a discussion on problems with ifstream and ofstream!! help me please!!! within the C++ Programming forums, part of the General Programming Boards category; I have a nice save function, except for a minor (well major) problem. There are no errors at compile or ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    problems with ifstream and ofstream!! help me please!!!

    I have a nice save function, except for a minor (well major) problem. There are no errors at compile or anything like that. But when I run the program and it gets to a certain line of code I get a windows error that says "blah.exe has encountered an error and must be closed. Sorry for any inconvenience.......". You know the rest. I have windows xp. This happened once before when I didnt initialize a variable, i think. Anyhow, there is nothing like that that could cause a problem. I've zeroed in on the lines where the windows error pops up:

    ofstream save("player.txt")

    save>>current_room->id>>"\n";

    save>>current_weapon->id>>"\n";

    there are 3 others. this is in main:

    Code:
    struct ROOM
    {
    ROOM* north;
    ROOM* south;
    ROOM* east;
    ROOM* west;
    int id;
    };
    
    ROOM chamber;
    ROOM hallway;
    chamber.north=&hallway;
    chamber.id=1;
    
    ROOM* current_room;
    
    current_room=&chamber.north;
    ...so current_room points to another variable of type room, which has an element that points to another variable of type room. this isnt the exact code, but its just about exact. i pass current_room to my saving function. when i do save>>current_room->id;, i get the windows error. It also happens with other variables that point to something. i've even tried saying something like:

    int id=current_room->id;

    and using 'id' instead of 'current_room->id'.that doesnt even work and it is a regular integer! i know that i can use current_room and things that it points to in functions because there are many others that do. this just makes no sense at all to me. when i get home i'll post the exact code, but i've pretty much explained it all. It makes no sense! help!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,675
    ofstream save("player.txt")

    save>>current_room->id>>"\n";

    save>>current_weapon->id>>"\n";
    If save is an ofstream object and you are writting stuff to it, don't you need to use << and not >>.
    struct ROOM
    {
    ROOM* north;
    ROOM* south;
    ROOM* east;
    ROOM* west;
    int id;
    };

    ROOM chamber;
    ROOM hallway;
    chamber.north=&hallway;
    chamber.id=1;

    ROOM* current_room;

    current_room=&chamber.north;
    chamber.north is a pointer as is current_room, so you probably just need to say current_room = chamber.north.
    i pass current_room to my saving function. when i do save>>current_room->id;, i get the windows error. It also happens with other variables that point to something.
    Again, maybe a case of using >> where you should be using <<.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    i know that i have all the sytax right for the structs and things, i was just writing taht code off the top of my head. also the << and >> are correct. i'll post the actual code later when i get home.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 10:40 AM
  2. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 07:34 AM
  3. ifstream and fileIn problems
    By jobolikescake in forum C++ Programming
    Replies: 8
    Last Post: 10-26-2002, 03:00 PM
  4. Replies: 9
    Last Post: 06-06-2002, 07:03 PM
  5. STL to: Cin, Cout, ifstream & ofstream
    By kuphryn in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 08:32 AM

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