1

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

3
  • You want std::getline Commented Sep 2, 2015 at 3:37
  • I'd go with your idea Commented Sep 2, 2015 at 3:38
  • You should remove that second vector from inside the loop.Its hiding the real vector. And don't access the vector to convert the number inside the loop, do that after the loop when the vector has some data. Commented Sep 2, 2015 at 4:45

1 Answer 1

3

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. 
Sign up to request clarification or add additional context in comments.

9 Comments

Awesome! This is exactly what I wanted to do. Thank you so much for your help. I will mark this as the best answer as soon as I have the option.
Nice answer, keeping the loop compact like that. For the benefit of people who (like me) just encountered the raw string literal R"...()..." for the first time, documentation is here.
Hi could you clarify one thing for me. I originally had some type casting, and that doesn't seem to work anymore. I've updated my original post to include that information.
I am also unable to cast using string stream, it also comes out to zero.
@JohnKraz You don't need to convert. Because segment2 is a number just make the variable an int rather than a string: int segment2;. Then the file operation will read it in as a number.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.