[英]How do I read a full name that is separated by a comma from a file in C++?
[英]How do I read a full name that is separated by a space from a file in C++?
我正在尝试从文件中读取以空格分隔的全名。 在示例中, name student
。 这是我的代码:
class student{
string name;
int degree;
int stage;
public:
void input(){
getline (my_file,name);
my_file>>degree;
my_file>>stage;
}
void display(){
cout<<name<<"\n"<<degree<<"\n"<<stage<<endl;
}
};
int main()
{
student S[3];
int len=0;
my_file.open("D:/h.txt", ios::in);
if (!my_file) {
cout << "No such file";
}
else
while (!my_file.eof()) {
S[len].input();
len++;
}
for(int i=0;i<3;i++)
S[i].display();
my_file.close();
return 0;
}
我的“h.txt”文件:
jack Donald
58
5
David William
82
4
Anthony Mark
89
3
我的问题是,它没有显示任何东西。
参考这个:
void input() {
getline(my_file, name);
string blah;
getline(my_file, blah);
degree = std::stoi( blah);
getline(my_file, blah);
stage = std::stoi(blah);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.