MarcL
MarcL

Reputation: 3593

Read csv and split on semicolon

I have to read in values separated by ";" from a csv file...e.g

2039213;Hans;Meier;12.20.1943;2.4;
4039293;Jim;Raynor;31.12.2011;3.0;

int;char[];char[];char[],float

how can I split one line into the words using not string , just char[] ? and then I have to put those splitted values into a struct, ok thats not too hard I think but how do I split values ? my code:

struct Studentendaten {
    int matrnr;
    string name;
    string vorname;
    string datum;
    float note;
};
Studentendaten stud;
array<Studentendaten,100> studArray ;   


  if (pos != -1) 
  {

      sub1 = sub.substr(0,pos);               
      sub2  = sub.substr(pos+1,pos);
      sub3  = sub.substr(pos+1,pos);
      sub4  =sub.substr(pos+1,pos);
      sub5  =sub.substr(pos+1,pos);           
      stud.matrnr = std::to_string(sub1);
      stud.name = sub2;
      stud.vorname = sub3;
      stud.datum = sub4;
      stud.note = float(sub5);
  }

  if (ch == '\n') 
  {
      stud = {matrn,name,vorname,datum,note};
      studArray[i] = stud;
      i++;
  }

I have also the problem conversion from string into int and from string into float doesnt work, no matter what function I apply to it... it often says :

dateiLesen.cc:54:19: error: 'to_string' is not a member of 'std'

or

 dateiLesen.cc:58:27: error: invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'float'

furthermore, I dont know what the problem with my struct is:

 dateiLesen.cc:13:9: note: main()::Studentendaten& main()::Studentendaten::operator=(const main()::Studentendaten&)
  struct Studentendaten {
 dateiLesen.cc:13:9: note:   no known conversion for argument 1 from '<brace-enclosed initializer  list>' to 'const main()::Studentendaten&'
dateiLesen.cc:13:9: note: main()::Studentendaten& main()::Studentendaten::operator=(main()::Studentendaten&&)
dateiLesen.cc:13:9: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'main()::Studentendaten&&'

Upvotes: 0

Views: 231

Answers (1)

benjymous
benjymous

Reputation: 2122

Ok, let's try and tackle those compile errors

dateiLesen.cc:54:19: error: 'to_string' is not a member of 'std'

You're not including the header that defines std::to_string, so the compiler has no idea what it is. [ hint: try google! ]

dateiLesen.cc:58:27: error: invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'float'

You can't cast a string into a float directly. You'll need to use something like boost::lexical_cast

Upvotes: 1

Related Questions