0

I haven't programmed in a while and am trying to work on a project which will read in tweets from a csv file and then manipulate what is stored. Currently, I am trying to just extract the data from the file and print to the console. I know that my file is opening because I had included a conditional statement, however, when it comes to reading the data rather than getting any actual information I am just getting blank lines. I also thought it could be because the csv file I am working with is quite large (20k data entries) so I added in a for loop to try and isolate the problem.

I am using getline and stringstream to get the data out, and cout to print to console. I just can't seem to find the issue.

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    string line;
    ifstream fin;
    fin.open("train_dataset_20k.csv");

    if (!fin.is_open()){
        cout << "Error opening file" << endl;
        return 1;
    }

    while(fin.is_open()){

        for (int i = 0; i <10; i ++){
            string Sentiment,id,Date,Query,User,Tweet;
            stringstream lineSS(line);

            getline(lineSS, Sentiment, ',');
            getline(lineSS, id, ',');
            getline(lineSS, Date, ',');
            getline(lineSS, Query, ',');
            getline(lineSS, User, ',');
            getline(lineSS, Tweet, '\n');

            cout << Sentiment << endl;
            cout << id << endl;
            cout << Tweet << endl;

        }

        fin.close();
    }

    return 0;
}

Currently, it will run through the for loop 10 times, but will just output blank lines with no information in it.

  • 1
    You aren't reading any data from your file stream....? Why are you using line? – Omid CompSCI Aug 29 '21 at 16:50
  • 1
    See also: [Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Thomas Matthews Aug 29 '21 at 18:27

1 Answers1

0

I know that my file is opening because I had included a conditional statement, however, when it comes to reading the data rather than getting any actual information I am just getting blank lines.

You never initialize the line variable anywhere inside your code so lineSS is initialized with an empty buffer.

Consequently, reading a stream with an empty buffer using std::getline() will give you an empty string, which is what you are facing.

To fix this, either put std::getline(fin, line); at the beginning of your second loop:

// ...

if (fin.is_open()) {

    for (int i = 0; i < 10; i ++) {
        getline(fin, line); // Reads each line from the file into the 'line' variable

        string Sentiment, id, Date, Query, User, Tweet;
        stringstream lineSS(line);

        getline(lineSS, Sentiment, ',');
        getline(lineSS, id, ',');
        getline(lineSS, Date, ',');
        getline(lineSS, Query, ',');
        getline(lineSS, User, ',');
        getline(lineSS, Tweet, '\n');

        cout << Sentiment << endl;
        cout << id << endl;
        cout << Tweet << endl;

    }

    fin.close();
}

// ...

Or just use the file stream directly without having to use a std::stringstream for mediation:

// ...

if (fin.is_open()) {

    for (int i = 0; i < 10; i ++) {
        string Sentiment, id, Date, Query, User, Tweet;

        getline(fin, Sentiment, ',');
        getline(fin, id, ',');
        getline(fin, Date, ',');
        getline(fin, Query, ',');
        getline(fin, User, ',');
        getline(fin, Tweet, '\n');

        cout << Sentiment << endl;
        cout << id << endl;
        cout << Tweet << endl;

    }

    fin.close();
}

// ...
Ruks
  • 3,886
  • 1
  • 10
  • 22
  • See also: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Thomas Matthews Aug 29 '21 at 18:26
  • @ThomasMatthews But the code doesn't use `std::ifstream::eof()` anywhere? What might you be referring to? – Ruks Aug 29 '21 at 18:29
  • The `while(fin.is_open()))`, which is not a good way to read files. – Thomas Matthews Aug 29 '21 at 18:31
  • @Ruks What Thomas is eluding to is that `getline(fin, line);` does not check if the read worked. – Martin York Aug 29 '21 at 18:34
  • @MartinYork Yes, if it is about checking the return value of `getline()`, I intentionally left it out in order to preserve the code as given in the question assuming that the OP knows what he is doing and makes sure that his CSV file has at least 10 entries. Of course, if needed, one can change the loop to `while (getline(fin, line))` which will take the check into account while reading each line. – Ruks Aug 29 '21 at 18:40