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:
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.