Thread: Reading from a file trouble

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    18

    Reading from a file trouble

    I'm having a hard time reading from a .txt file. Here's a short version of what I have:

    Code:
    LoadFile.open("Save File.txt", ios::in);
    
      getline(LoadFile, name1);
    
        for (int i = 0; i<2;i++)
            Loadfile >> data[i];
    
    
      getline(LoadFile, name2);
    
    LoadFile.close();
    
    
    cout << name1 << endl;
    cout << data[0] << endl;
    cout << data[1] << endl;
    cout << name2 << endl;

    Here's what the txt. looks like

    Code:
    Bob Smith
    50
    100
    Sam Smith
    And here's what gets displayed

    Code:
    Bob Smith
    50
    100
    For some reason, name2 is always empty. Why does getline only work the first time? I tried just doing LoadFile, but then it doesn't grab the Last name because of the space.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The second getline is reading the newline left by the last Loadfile >> data[i] input.

    Mixed input methods is always a disaster waiting to happen at some point.

    Typically, I would use getline to read ALL the input into a temp buffer, then do whatever was necessary to process that to its intended destination.
    The string stream extraction operations are a good idea to look into.

    A quick hack would be to use the stream .ignore() method when you switch from >> extractions back to getline. Use this to skip chars upto the next newline.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by Salem View Post
    The second getline is reading the newline left by the last Loadfile >> data[i] input.

    Mixed input methods is always a disaster waiting to happen at some point.

    Typically, I would use getline to read ALL the input into a temp buffer, then do whatever was necessary to process that to its intended destination.
    The string stream extraction operations are a good idea to look into.

    A quick hack would be to use the stream .ignore() method when you switch from >> extractions back to getline. Use this to skip chars upto the next newline.
    Thank you very much. The .ignore did the trick. I did not know mixing input methods would be such a hassle, next time I will look into reading the whole file first ,then sorting it out.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    18
    My save file is getting pretty complicated. It saves strings, ints, and floats.
    Having it write the information I want to a .txt is no sweat.

    However, reading the various data types is becoming very difficult.
    How would I code a function that could read:

    Farmer Bob
    //FRUIT
    Apple
    Pear
    Peach
    //MONEY
    100

    And sort it out so the line before //Fruit is saved to a string, and each line until //MONEY is saved into a string array, and everything until the end is saved to an int?

    Could you point me in the direction to learn intermediate file I/O and how to work with a temp buffer for sorting?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well try to specify what your file format is like. From what I see

    You have a couple of constraints. Constraints are rules that code should never break.
    1. The first line is the Farmer's name.
    2. Items after the line "//FRUIT" are strings of fruit names.
    3. Items after "//MONEY" are currency.
    4. Currency is expressed as an integer.

    With this information you can then devise an algorithm. Such as, read the farmer's name, store it somewhere. The next line needs to be //FRUIT or //MONEY. If the section is fruit, place each item in an array until a line starts with //. If the section is //MONEY, place the amounts in an array as they appear, so they are parallel with the fruit names. The file has been read after EOF is reached.

    Be as detailed as you require. Making a plan makes all things simpler.

    As for working with a temp buffer for sorting -- well most sorting algorithms work in-place, so I don't understand your request.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM