0

I have string in text file with comma separated value and i am reading comma separated value in different variable as doing below

ifstream file("example.txt");
int age, count;
char name[50]="\0";
char degree[50] = "\0";     
while (file.getline(name, 50, ','))
{
    file >> age;
    file >> count;
    file >> degree;
    cout << name << age << count<<degree <<endl;
}

It assign value to name and age but not to count and degree. Can anyone guide me what i am doing wrong? Comma separated strings are below.

waqar ahmed,17,5,bscs
usman ahmed,17,6,mscs
Azeem Hafeez
  • 250
  • 4
  • 14
  • 1
    The first call to `getline` reads everything up to the first comma, which would be "waqar ahmed", and puts it into `name`. How do you expect the rest of the values to be separated by the remaining commas? `>>` doesn't do that, of course. In any case, the shown approach is fundamentally flawed, and won't work. You need to implement a proper CSV parser. – Sam Varshavchik Jun 18 '20 at 12:25
  • Use `std::string`. It works well with `std::getline`. – Thomas Matthews Jun 18 '20 at 15:25

0 Answers0