C++怎样把文本文件中的学生成绩信息读入到一个student类的数组中 用ifstream getlineinfile读入

c++读入文本文件_百度知道
c++读入文本文件
1. 设计一个学生类student,包括学生学号、姓名、成绩;设计一个友元函数,比较某两个学生成绩的高低;读入一个文本文件(格式如示例studengt.txt,每行的学号、姓名、成绩之间用四个空格隔开)中所有学生的学号、姓名、成绩,输出最高成绩和最低成绩的学生信息(学号、姓名、成绩)
提问者采纳
// trystudent.cpp : 定义控制台应用程序的入口点。 // #include &stdafx.h& #include &fstream& #include &iostream& #include &iomanip& #include &string&
class student { public:
friend int greater2(student s1,student s2); friend ostream & operator&&(ostream & out,student &s); }; ostream &operator&&(ostream & out,student &s) { out&&setw(10)&&s.no&&setw(10)&&s.name&&setw(5)&&s.score&&
} int greater2(student s1,student s2) { return s1.score&s2. } void readfile(student * stu,int &count) { count=0;
ifstream ifs(&student.txt&); while(!ifs.eof()) { ifs&&stu[count]. ifs&&stu[count]. ifs&&stu[count]. count++; } ifs.close(); } int _tmain(int argc, _TCHAR* argv[]) { student stu[10]; int count=0; readfile(stu,count); if(count==0)return 0; student max, max=min=stu[0]; for(int i=0;i&i++) if(greater2(stu[i],max))max=stu[i]; else if(greater2(min,stu[i]))min=stu[i]; cout&&&最大分数的学生&&& cout&&&最小分数的学生&&& return 0; }
其他类似问题
文本文件的相关知识
您可能关注的推广
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C++编写简单的学生信息管理系统,但是文件读入是只有第一行是成功的,求解决方法_百度知道
C++编写简单的学生信息管理系统,但是文件读入是只有第一行是成功的,求解决方法
系统的具体要求是查询某科目的所有学生成绩和平均成绩,还有能查询某个学生的所有学科成绩和平均成绩。但是我调试发现只有文件第一行读进去了,之后的就没有了。。。还有平均成绩不用算,我写在文件里了。。。代码如下:#include &iostream&#include &fstream&#include &stdlib.h&int main(){
int sum=0,ave=0,i=0,n=0;
char input[4];
ifstream in(&stu.txt&);
struct student{
char name[4];
while(in){
in.get(stu[n].name,4);
in&&stu[n].
in&&stu[n].
in&&stu[n].
in&&stu[n].
cout&&&enter a name:&;
cin.get(input,4);
for (i=0;i&=n;i++){
if(input==stu[i].name) cout&&stu[i].name&&& &&&stu[i].chi&&& &&&stu[i].math&&& &&&stu[i].eng&&& &&&stu[i].ave&&
cout&&&enter a subject:&;
for(i=0;i&=n;i++){
if(input==&chi&) cout&&stu[i].name&&stu[i].chi&&
if(input==&math&) cout&&stu[i].name&&stu[i].math&&
else cout&&stu[i].name&&stu[i].eng&&
system(&pause&);
return 0;}这个写了两天了还没搞定我已经不会好了_(:з」∠)_求大神解答
粗略观察了一下,大概是文件指针没有移动到第二行。我推测stu.txt的中的数据是按行存放的,一行一组数据。这样读完第一行数据(然后是回车,不可读),文件指针无法跳转到第二行,所以stu[1]到stu[3]都没有数据。你可以用in.getline()获取一整行的数据,然后Iin&&stu.name…………另外,比较好的做法是,把数据以结构体的方式存放进stu.txt,然后用read(&stu,)读出结构体。这样不需要考虑文件指针
lin&&stu.name是什么意思?还有数据怎么样以结构体的方式存进stu.txt?
写错了,不好意思,是in&&stu.name。百度“把结构体写入文件流”
getline的参数应该怎么写?我怎么试都报错。。。
需要头文件string
其他类似问题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 ifstream getline 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信