Thread: Reading a line from a text file Please help

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Arrow Reading a line from a text file Please help

    i am coding a game, and trying to find out to save the game, load the game, keep inventory, etc.

    I want to read from those files, saving them is working, and set the things according to that, however, i would need to read that line, and set the stats from that line. I only know how to display the entire file, and i can do it in binary, but i find binary to be a pain in the butt.

    Please help!

    I do not wish to make a seperate file for each thing, but even then, using ifstream fin, i can only read one character at a time, making them into a actual string is a different story.

    Thanks in advance!
    This war, like the next war, is a war to end war.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Use some flavor of getline:
    Code:
    string data;
    
    ...
    
    getline(fin, data);
    Code:
    char data[SIZE];
    
    ...
    
    fin.getline(data, sizeof (data));
    p.s. What the alphabet would look like without q and r.

  3. #3
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    well, im using it for a text adventure MUD like thing. I am using files to keep inventory and saving. Many of the items are multi worded, like the only thing worn at the begin, Tattered Cloth Pants, when i see the equipped items, it says:

    Tattered
    Cloth
    Pants,

    though the file says Tattered Cloth Pants.

    Oh, and, if anybody would like to help me make this adventure a really good one, please PM me.

    The reason for this is its gonna be a grid of bout 300x300 or more.
    Needs lots of files.....

    But anyway, can you maybe give me an example of that, brighteyes?

    Im not the best for understanding code without examples or heavy explanation.

    heres what im using:
    Code:
    void save()
    {
     ofstream save("savefiles/Savegame.sav");
     save << p.name << endl;
     save << p.hp << endl;
     save << p.mana << endl;
     save << p.dmg << endl;
     save << p.def << endl;
     save << p.lvl << endl;
     save << p.exp << endl;
     save << p.expneed << endl;
     save << p.y << endl;
     save << p.x << endl;
     save << mission << endl;
    }
    
    void load()
    {
    ifstream load("savefiles/Savegame.sav");
    load >> p.name;
    load >> p.hp;
    load >> p.mana;
    load >> p.dmg;
    load >> p.def;
    load >> p.lvl;
    load >> p.exp;
    load >> p.expneed;
    load >> p.y;
    load >> p.x;
    load >> mission;
    }
    not the best, but it works.
    This war, like the next war, is a war to end war.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Tattered
    >Cloth
    >Pants,
    >though the file says Tattered Cloth Pants.
    The >> operator stops reading at whitespace. As Brighteyes mentioned, it's better to read an entire line from the file and then split it up as you see fit with logic in your program. That way you have control over what goes where.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I believe you can write/read an entire struct/class object from file if you want, rather than writing/reading in each parameter one at a time. look up the write() method of ofstreams and the read() method of ifstreams if you want to look into this more. IF I remember correctly, the drawback is the file is in binary, not text, but at least it's writing/reading entire objects.

  6. #6
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    I want it in text so that i can edit game saves
    This war, like the next war, is a war to end war.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Go here:

    http://www.stud.fim.ntnu.no/~oystesk....htm#Heading61

    It is an example of writing a class object to file and back again all at once. The file is stored in binary, but when used in program it is in text so it can be modified as you would normally do. The only difference would be if you saved it to filename myData.txt and tried to open it with a text editor like NotePad you are likely to see gibberish rather than letters you are used to. However, the gibberish will be translated back to readable/useable version when read back to file by read(). Just read through the example to see how it's done.

  8. #8
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    okay, ill look at it, thanks.

    Sooo, will any one help me with this game?

    I really need someone to help me learn internet, but first i want to get the rest working.

    I will eventually make it an interactive MUD, but not until i get that much needed help for it.
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  2. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 02:30 PM
  3. end of line in text file
    By spveer in forum C Programming
    Replies: 5
    Last Post: 08-18-2005, 12:43 AM
  4. reading the next line in a text file
    By colorado_scott in forum C Programming
    Replies: 12
    Last Post: 03-31-2003, 04:38 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM