0

Assume a file containing

a123,red,23,Greece,"traveling, in cities", "playing games, like cod"
b245,"ted",45,London,"cars", "Exotic cars, loves Lambo"

I have been trying to read the file and store it in vector of class. The class has objects string Id, string name, int age, string from, vector Likes.

The file should store in vector in form of

vector<class> Info = {
                     {"a123","red",23,"Greece",{"traveling, in cities", "playing games, like cod"},
                     {"b245","ted",45,"London",{"cars", "Exotic cars, loves Lambo"}}

Can anyone help and tell me what I can use to read file, which is filled with quotation and commas in random places and is comma delimited into the vector in form shown above. thank you

  • Does this answer your question? [How can I read and parse CSV files in C++?](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) This is much like a csv file. – prehistoricpenguin Apr 13 '21 at 09:09

2 Answers2

0

Suppose objects in your vector are of class User. There are two scenarios here,

  1. The file is not written by a c++ program that know about User: You should look into deserialisation and serialisation.

  2. The file I not written by a c++ program that knows about User: You have to read the file and create objects of type User and add them to a vector. You can read file as described here

0
  • Preliminarily, to construct its own structure, each structure is composed of the above member variables

  • The second step is to understand the data structure in the file, so that you can know how many data can be read each time to form one structure

  • The third step is to read the file, with a comma as the separator or something else,split the stream and get what you need.

stuct user_data {
  string Id,
  string name,
  int age,
  string from,
  vector Likes
};

std::vector<user_data> *GetUserDataFromFiles(FILE *file) {
    // spilt string from file 
}