c++stringc++11vectoristringstream

How to skip the space and the new line from stringstream and put them in a vector?


I'm trying to read a text file that contains information of a person (name, age, occupation) as follows:

name 20
occupation
name 25
occupation
name 34
occupation

I read the whole file and for each line I used istringstream to skip the space between the name and the age.

std::vector<string> readfile(std::vector<std::string> *words, char *argv){

    std::ifstream file(argv);   //Opens the file specified on execution

    if ( !file ){
        cerr << "Le fichier " << argv << " n'existe pas" << endl;
        exit (-1);
    } else {
            std::string line;
            while (std::getline(file, line)){
                        istringstream ss(line);
                        do {
                                string word;
                                ss >> word;
                                if (word != " " || word != "\n"){
                                        words->push_back(word);
                                };
                        } while (ss);
                };
                file.close();
        };
        return *words;
};

My main is:

int main( int argc, char *argv[] ){
        std::vector<std::string> compV;

        readfile(&compV,argv[1]);
        cout << compV.at(2) << endl;
        
        return 0
}

When I compile and execute the program, I get a space as the result. compV.at(0) shows name comV.at(1) shows age but comV.at(2) shows a space instead of occupation.

What did I do wrong here ?


Solution

  • You can do

    string mystr;
        
    while(file >> myStr) {
      string name = mystr;
      file >> mystr;
      int age = stoi(mystr);
      file >> mystr;
      int occupation = stoi(mystr);
    }
    

    As long as you know the order of information you are getting from your file you can follow the idea above.

    When you do file >> mystr it will get the next word/number until the white space, Once you get the name, next info in this case will be age, after that end of line so it will move down and do the same process again until end of file.

    With getline you are getting the whole line.

    Here is an example program. This is the .txt file

    ADD A 1 2 3 4 5 6 7 8 9 10 11 12 STOP
    ADD B 4 6 8 10 12 14 16 STOP
    

    And the program

       SetT<int> a;
       SetT<int> b;
       string mystr, str;
       ifstream testFile;
       testFile.open("testDrive.txt");
            
       if(testFile){
           while(testFile >> mystr){
               if(mystr == "ADD"){
                   testFile >> mystr;
                   if(mystr == "A"){
                        while(testFile >> mystr && mystr != "STOP"){
                            stringstream(mystr) >> num;
                            cout << "A : ";
                            a.Add(num);
                        }
                    } else {
                        while(testFile >> mystr && mystr != "STOP"){
                            stringstream(mystr) >> num;
                            cout << "B : ";
                            b.Add(num);
                        }
                    }
                        
               }
            }
        }