I'm trying to read in a csv file and then use what is read in to create objects. These objects will form a linked list.
When I open the csv file in notepad it looks something like this:
Name,Location Bob Smith,Los Angeles Joe Smow,New York Generic Name,Phoenix
I want to skip over the first row (Name,Location) and read in the rest.
Right now my code looks like this:
ifstream File("File.csv");
string name, location, skipline;
if(File.is_open())
{
//Better way to skip the first line?
getline(File, skipline, ',');
getline(File, skipline);
while (File.good())
{
getline(File, name, ',');
getline(File, location);
//Create new PersonNode (May not need the null pointers for constructor)
PersonNode *node = new PersonNode(name, location, nullptr, nullptr);
//Testing
cout << node->getName() << " --- " << node->getLocation() << endl;
//Add HubNode to linked list of Hubs (global variable hubHead)
node->setNext(hubHead);
hubHead = node;
}
}
else
{
cout << "Error Message!" << endl;
}
This seems to read in the file OK for the most part, but is there a better way to skip the first row? Also, when the file is printed out the second row of the last column is duplicated so it looks like this:
Input:
Name,Location Bob Smith,Los Angeles Joe Smow,New York Generic Name,Phoenix
The output is:
Bob Smith --- Los Angeles Joe Smow --- New York Generic Name --- Phoenix --- Phoenix
If it is relevant, the constructor for the objects looks like this (the OtherNode is going to be used because another linked list will involved, but I'm not worrying about that yet).
PersonNode::PersonNode(string name, string location, Node *next, OtherNode *head) {
PersonNode::name = name;
PersonNode::location = location;
PersonNode::next = next;
PersonNode::OtherNode = OtherNode;
}
Thanks for any help, it is greatly appreciated.