首页 > 解决方案 > c ++在变量中读取逗号分隔的文件

问题描述

我在文本文件中有带有逗号分隔值的字符串,我正在读取不同变量中的逗号分隔值,如下所示

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;
}

它为 and 赋值nameage但不为countand赋值degree。谁能指导我做错了什么?逗号分隔的字符串如下。

waqar ahmed,17,5,bscs
usman ahmed,17,6,mscs

标签: c++file-handling

解决方案


推荐阅读