0

I'm working on a project using C++, and I was asked to read a txt file and assign the data to a node. The text file is:

Yani, 19, 1993, 12345

Oscar, 20, 1994, 56789

And I have the struct List:

struct List{
    string name;
    int age;
    int birth;
    int id;
    List *next;
    List *prev;
}

My question, how can I assign the data from the text file to the nodes, in this case, two nodes, but if there are more lines, it has to create more new nodes?

Part of my code I'm using:

#include <fstream>

struct List{
...
}

//Here I create the first Node and:
aux=head;
ifstream file ("file.txt");
file >> aux->name >> aux->age >> aux->birth >> aux-> id;

If the text file would not have the commas in it, works perfectly, but the commas make errors everywhere.

Also, to conclude :) If the text file has:

Danny Watson, 23, 1980, 58953

The name of the node must be Danny Watson, not only Danny :)

I hope you can help me! I'll be so grateful.

(Sorry, my English is not that good :( )

  • 1
    What is the error with the commas? That they are included in the result? – Javi Sep 12 '14 at 22:32
  • Yes, and I don't want them to be included in the "name" or "age" variables, because I have to show the nodes using another function. Thanks in advance. – I love programming with coffee Sep 13 '14 at 01:09
  • You could try searching StackOverflow for "c++ read text file list" for a plethora of examples. – Thomas Matthews Sep 13 '14 at 01:12
  • My bad, you are reading a "Comma Separated Values" file, so you need to search StackOverflow for "C++ read file csv" and look at the results involving text fields. My previous suggestions are for non-csv files (such as using tabs instead). – Thomas Matthews Sep 13 '14 at 01:19
  • Thank you @Thomas, that is what a friend said to me, to look up about CSV files, but I'm a totally begginer and barely understand the syntax used. The task says it has to be a struct List and the variables must be assigned to a node. New line is for another new node. searching StackOverflow for "C++ read file csv" will help me? :) Thank you again! – I love programming with coffee Sep 13 '14 at 01:30
  • Your issue is with reading values from the file into a node, not with the list. Figure out how read the file into variables. You can create a node from the variables. All the references from the search show how to do read from the file into variables. – Thomas Matthews Sep 13 '14 at 01:33

1 Answers1

3

The problem is that the input operator >> reads a whitespace delimited "word". That means an input such as "Danny Watson" will be read as two separate words.

Instead I suggest you use std::getline to read the whole line into a stdstring, then you use std::getline again with an std::istringstream to get the comma-separated values. Remember to strip leading (and possible trailing) whitespaceif and when needed.

Something like this:

std::vector<List> myList;

std::string line;
while (std::getline(file, line))
{
    std::istringstream iss(line);

    List myEntry;

    // Read a comma-separated entry from the temporary input stream
    std::getline(iss, myEntry.name, ',');

    std::string temp;

    std::getline(iss, temp, ',');
    myEntry.age = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.birth = std::stoi(temp);

    std::getline(iss, temp, ',');
    myEntry.id = std::stoi(temp);

    myList.push_back(myEntry);
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621