我如何读取由c++中的文件分开的全名?
我正在尝试阅读由空间与文件分开的全名。在示例中,名称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
我的问题是,它没有显示任何内容。
I am trying to read full names that are separated by space from a file. In the example, name student
. Here is my code:
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;
}
my "h.txt" file:
jack Donald
58
5
David William
82
4
Anthony Mark
89
3
My problem is, it don't show anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参考:
Refer this: