JohnKraz
JohnKraz

Reputation: 129

Reading Data from a text file with fstream

I am having some trouble read and working with a data file. There are 3 categories that I will create from the data file. The first two categories are each based off data that is guaranteed not to be split up. The third category may be split up a variable number of times.
The code below is the process I am currently using. This gets the job done when each segment is just one part (ex. segment3 = "dog"), but I need the application to be able to handle a variable number of parts for segment3 (ex. segment3 = "Golden Retriever" or "Half Golden Half Pug"). segment1 and segment2 are guaranteed to be whole and not split between spaces. I understand why my code skips over any extra spaces (Instead of recording "Golden Retriever" it will only record "Golden". I don't know how to manipulate my code so that it understands that anything anything in the line after segment2 is a part of segment3.

 ______________________________
// This is the structure of the data file. It is a .txt
China 1987 Great Wall of China.
Jordan 1985 Petra.
Peru 1983 Machu Picchu. 
// End of Data file. Code below.
________________________________

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
    ifstream myFile("data.txt");
    string segment1;
    string segment2;
    string segment3;
    vector <string> myVec;

    while(myFile >> segment1 >> segment2 >> segment3)
    {
        vector <string> myVec; // 
        myVec.push_back(segment1); myVec.push_back(segment2); myVec.push_back(segment3); 
    int Value = atoi(myVec[1].c_str()); // should print int value prints zero with getline 
    }

    return 0;
}

I have searched stackoverflow and the internet, and found some ideas but nothing that seems to help address the issue while working with my code. The best idea that I have would involve scrapping my current approach to reading the file. 1. I could parse the data using getline and into a vector. 2. I could assign index 0 to segment1 and index 1 to segment2. 3. I could assign index 3 until the end of the vector to segment 3.


Galik's solution helped me resolve that, but now I have an issue attempting to type cast. [int altsegment2 = atoi(segment2.c_str());] always results in zero now

Upvotes: 1

Views: 801

Answers (1)

Galik
Galik

Reputation: 48635

You can use std::getline to read the entire rest of line like this:

#include <iostream>
#include <fstream>
#include <sstream> // testing
#include <vector>
using namespace std;

int main()
{
    // for testing I substituted this in place
    // of a file.
    std::istringstream myFile(R"~(
    China 1987 Great Wall of China.
    Jordan 1985 Petra.
    Peru 1983 Machu Picchu. 
    )~");

    string seg1;
    string seg2;
    string seg3;
    vector<string> v;

    // reads segments 1 & 2, skips spaces (std::ws), then take
    // the rest of the line into segment3
    while(std::getline(myFile >> seg1 >> seg2 >> std::ws, seg3))
    {
        v.push_back(seg1);
        v.push_back(seg2);
        v.push_back(seg3);
    }

    for(auto const& seg: v)
        std::cout << seg << '\n';

    return 0;
}

Output:

China
1987
Great Wall of China.
Jordan
1985
Petra.
Peru
1983
Machu Picchu. 

Upvotes: 3

Related Questions