-3

I've been struggling with parsing a csv file so that every new line is read as a new row. Unfortunately, I couldn't find an answer in the SO forums that addressed my problem. I opened the csv file with textEdit and saw this:

enter image description here

I think the problem is how to tell the compiler when to break off a new line. For example, right now, it will read z (row 2) as being on the previous row. Please take a look at my code and output below (i am using Xcode v 9.2).

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

int main()
{
    ifstream ip("ebid2016.csv");
    if (!ip.is_open()) {
        cout << "can't open file" << '\n';
    }
    string title;
    string articleId;
    string department;
    string closeDate;
    string bid;
    string inventoryId;
    string vehicleId;
    string receiptNum;
    string fund;
    string nothing;


    while(ip.good()){

        getline(ip,title,'\t');
        getline(ip,articleId,'\t');
        getline(ip,department,'\t');
        getline(ip,closeDate,'\t');
        getline(ip,bid,'\t');
        getline(ip,inventoryId,'\t');
        getline(ip,vehicleId,'\t');
        getline(ip,receiptNum,'\t');
        getline(ip,fund,'\t');

        cout << "Article Title:" << title << '\n';
        cout << "Article ID:" << articleId << '\n';
        cout << "Department:" << department << '\n';
        cout << "Close Date:" << closeDate << '\n';
        cout << "Bid:" << bid << '\n';
        cout << "Inventory ID:" << inventoryId << '\n';
        cout << "Vehicle ID:" << vehicleId << '\n';
        cout << "Receipt Number:" << receiptNum << '\n';
        cout << "Fund:" << fund << '\n';

        cout << "------------------" << '\n';
    }
    ip.close();
}

Output:

Article Title:ArticleTitle
Article ID:ArticleID
Department:Department 
Close Date:CloseDate 
Bid:WinningBid 
Inventory ID:InventoryID
Vehicle ID:VehicleID
Receipt Number:ReceiptNumber 
Fund:Fund
z <------------why does z show up here?
------------------
Article Title:97991
Article ID:POLICE PROPERTY AND EVIDENCE UNCLAIMED
Department:12/1/16
Close Date:$27.00 
Bid:PPEU-031C-149
Inventory ID:
Vehicle ID:3689905552
Receipt Number:Enterprise
Table
Fund:97990
------------------ etc.
I Like
  • 1,711
  • 2
  • 27
  • 52

1 Answers1

1

I think the fix is:

getline(ip,fund,'\n');

You are reading the Fund field until it reaches the next tab character (\t), when in actuality you want to read to the next newline.

pseudocoder
  • 4,314
  • 2
  • 25
  • 40
  • Thanks. Your approach worked for the first row, but the rest of the rows lost all their formatting and were printed as is: eg `z,97991,POLICE PROPERTY AND EVIDENCE UNCLAIMED,12/1/16,$27.00 ,PPEU-031C-149,,3689905552,Enterprise` – I Like Jan 10 '18 at 21:45
  • Sounds like a new problem and a new question. Considering the downvotes, I suggest you try a new question and don't say you want to "parse CSV" since you are parsing a tab delimited text file in a specifically formatted way. – pseudocoder Jan 19 '18 at 21:09