-1

I have a csv file that looks like this:

Start Time(s) Start Floor End Floor 41 54 55 43 30 74 88 87 32 93 35 55 121 35 4

I am trying to read the csv line by line, and print out:

Current Start Time: 41
Current Start Floor: 54
Current End Floor: 55

Current Start Time: 43
Current Start Floor: 30
Current End Floor: 74
...etc

Instead, I am getting:

Current Start Time: S
Current Start Floor: t
Current End Floor: a


Current Start Time: 4
Current Start Floor: 1
Current End Floor: ,


Current Start Time: 4
Current Start Floor: 3
Current End Floor: ,


Current Start Time: 8
Current Start Floor: 8
Current End Floor: ,

Why am I not printing out the three values in the columns? How can I properly access the columns of what I am reading in? (I can either read it in line-by-line, or all at once, and access the rows, column by column, later on)

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

int main(){
    //cout << "Hello world" << endl;

    string line; // for reading in the file as a string

    // open the .csv file
    ifstream fin("D:\\Grad School\\Fall 2019\\EN.605.604.81.FA19 - OO Programming with C++\\Project 7\\data.csv");
    // while it still has lines
    while(getline(fin, line)){
        // set curr to the current value
        cout << "Current Start Time: " << line[0] << endl;
        cout << "Current Start Floor: " << line[1] << endl;
        cout << "Current End Floor: " << line[2] << endl;
        cout << "\n" << endl;
    } //end while

}
artemis
  • 6,857
  • 11
  • 46
  • 99
  • 1) This is not a CSV file, 2) Which part of the shown code you believe parses each line in the file in the manner you're looking to do? `line[0]` is only the first character of each line, `line[1]` is the 2nd, and `line[2]` is the third. You told the computer to show the first three characters of each line, and according to the Golden Rule Of Computer Programming ("A computer always does exactly what you tell it to do instead of what you want it to do"), this is exactly what you see. – Sam Varshavchik Nov 07 '19 at 02:54
  • Right...it's a markdown table that contains the data that is in the CSV file, should be pretty representative. Also, a quick scan would probably point you to where I explicitly print out `Current Start Time: ` as an attempt to read in the value that is in the `Start Time`. Clearly, it is doing exactly as I asked, but I don't understand how to fix it, and have not found resources on this library of Q&A to fix it. – artemis Nov 07 '19 at 02:56
  • Well, how to make your question clearer would be, first of all, to show the actual input provided to your program, instead of some transformed representation of it. What's being described here as a "csv file that looks like this", does not actually look like a csv file. As far as "how to fix it", that would be to actually write code that parses a CSV-formatted file. There is [a canonical question on stackoverflow](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) on this topic. Do you agree that your question should be closed as dupe of it? – Sam Varshavchik Nov 07 '19 at 03:11
  • ... and someone just proved my point for me. Someone posted an answer here that appears to attempt to parse what's actually shown in the question. Instead of an actual CSV file. – Sam Varshavchik Nov 07 '19 at 03:22
  • I used the same question you referenced to first learn how to read in a .csv file, but if I am missing where in that question any answer explains how to parse columns into variables, then please close this as a duplicate. My desire to better SO exceeds my desire to solve my problem in a timely fashion. In most `python` tags, the format above is used to represent .csv files, as I have in many questions, and as I have in many answers. Sorry it did not meet your expectations; I will edit now. – artemis Nov 07 '19 at 03:27
  • If you examine the canonical question, you will observe that the examples given in its answer simply save the value from each column into a single vector. You simply replace that part of the given solution to save each value in the appropriate variable, instead. Mission accomplished. The examples in the other question clearly parse each column's value, and save each value in a `std::vector`. You just take that part and save it into each variable, adding a little bit of logic that does that. – Sam Varshavchik Nov 07 '19 at 03:30

1 Answers1

1

Generally, if your records are separated by newlines, you'll want to read in the record as a string and parse the string.

struct Record
{
    int start_time;
    int start_floor;
    int end_floor;
    friend std::istream& operator>>(std::istream& input, Record& r);
};

std::istream& operator>>(std::istream& input, Record& r)
{
    std::string record_text_line;
    std::getline(input, record_text_line);
    char separator;
    std::istringstream text_stream(record_text_line);
    // Read first separator
    text_stream >> separator;
    // Read in first value
    text_stream >> r.start_time;
    text_stream >> separator;
    text_stream >> r.start_floor;
    text_stream >> separator;
    text_stream >> r.end_floor;
}

In your main function, you'll need to read the first two lines and ignore them.

std::string text_record;
std::vector<Record> database;
Record r;
//...
std::getline(fin, text_record);  // Read & ignore first line.
std::getline(fin, text_record);  // Read & ignore second line.

// Read in the data.
while (fin >> r)
{
    database.push_back(r);
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154