Dragoness
Dragoness

Reputation: 11

Trouble with parsing text file based on commas (C++)

I am working on creating a program that is supposed to read a text file (ex. dog, buddy,,125,,,cat,,,etc...) line by line and parse it based on commas. This is what I have so far but when I run it, nothing happens. I am not entirely sure what i'm doing wrong and I am fairly new to the higher level concepts.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
    std::ifstream file_("file.txt"); //open file 
    std::string line_; //declare line_ as a string
    std::stringstream ss(line_); //using line as stringstream
    vector<string> result; //declaring vector result

    while (file_.is_open() && ss.good())
    { //while the file is open and stringstream is good
        std::string substr; //declares substr as a string
        getline( ss, substr, ',' ); //getting the stringstream line_ and substr and parsing
        result.push_back(substr);
    }
    return 0;
}

Upvotes: 1

Views: 58

Answers (1)

eesiraed
eesiraed

Reputation: 4654

Did you forget to add a line like std::getline(file_, line_);? file_ was not read from at all and line_ was put into ss right after it was declared when it was empty.

I'm not sure why you checked if file_ is open in your loop condition since it will always be open unless you close it.

As far as I know, using good() as a loop condition is not a good idea. The flags will only be set the first time an attempt is made to read past the end of the file (it won't be set if you read to exactly the end of the file when hitting the delimiter), so if there was a comma at the end of the file the loop will run one extra time. Instead, you should somehow put the flag check after the extraction and before you use the result of the extraction. A simple way is to just use the getline() call as your loop condition since the function returns the stream itself, which when cast into a bool is equivalent to !ss.fail(). That way, the loop will not execute if the end of the file is reached without extracting any characters.

By the way, comments like //declaring vector result is pretty much useless since it gives no useful information that you can't easily see from the code.

My code:

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
int main()
{
    std::ifstream file("input.txt");
    std::string line, word;
    std::vector<std::vector<string>> result; //result[i][j] = the jth word in the input of the ith line
    while(std::getline(file, line))
    {
        std::stringstream ss(line);
        result.emplace_back();
        while(std::getline(ss, word, ','))
        {
            result.back().push_back(word);
        }
    }
    //printing results
    for(auto &i : result)
    {
        for(auto &j : i)
        {
            std::cout << j << ' ';
        }
        std::cout << '\n';
    }
}

Upvotes: 1

Related Questions