请教大家 写/读文件问题(只有10分了)希望大家指教

YOU2005 2006-09-01 10:06:52
想把学生成绩写入后,我该如何读取学生的成绩,如果我输入10个学生成绩,我该如何读取这10个学生的成绩呢?

#include <iostream.h>
#include <fstream.h>
#include <string.h>
struct student{
char name[12];
char no[2];
float chengji[3];
};


void main()
{
fstream a;
student bb,qq;
//写入学生信息;
strcpy(qq.name,"xiao ming");
strcpy(qq.no,"1");
qq.chengji[0]=100;
qq.chengji[1]=95;
qq.chengji[2]=90;
//写入文件;
a.open("d:\\abc.txt",ios::out);
a<<qq.chengji[0]<<endl;
a<<qq.chengji[1]<<endl;
a<<qq.chengji[2]<<endl;
a<<qq.name<<endl;
a<<qq.no<<endl;
a.close();

}
...全文
183 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
toucher 2006-09-02
  • 打赏
  • 举报
回复
你上面的有些错,且输入后成绩没保存进文件里,输出也根本不是从文件中输出.我改后并整理如下:(我也是用VC啊,getline对编译器都可以用的啊)
#include <iostream>
#include <fstream>
#include <string>
using namespace std ;

struct chengji{
char name[30];
char no[30];
float yu;
float shu;
float ying;
} ;

int i = 0 ;
void xstudent(chengji qq) //写入文件
{
ofstream a;
a.open("d:\\abc.txt",ofstream::app );
a << "第" << i << "个学生的成绩" << endl ;
a<<qq.name << endl ;
a<<qq.no << endl ;
a<<qq.yu << endl ;
a<<qq.shu << endl ;
a<<qq.ying << endl ;


a.close();

}

void dstudent() //读取文件
{
cout << "全部学生成绩为:" << endl ;
fstream a;
a.open("d:\\abc.txt",ios::in);
string str ;
while (getline (a, str))
{
cout << str << endl ;
} //直到文件尾部

a.close();
}



void putdata ()
{
chengji bb ;
cout<< "请输入第" << ++i << "个学生的成绩"<< endl;
cout<<"输入姓名:";
cin>>bb.name;
cout<<"输入学号:";
cin>>bb.no;
cout<<"输入语文:";
cin>>bb.yu;
cout<<"输入数学:";
cin>>bb.shu;
cout<<"输入英语:";
cin>>bb.ying;
xstudent(bb);

}

void main()
{
//a<<qq.yu ;
while (1)
{
cout << "选择'y'输出学生成绩,选择'n'退出并答应成绩" << endl ;
char c ;
cin >> c ;
if (c == 'y')
{
putdata () ;
}
else
{
break ;
}
}

//chengji bb;

/*for (int i=0;i<1;i++)
{
cout<<"输入姓名:";
cin>>bb.name;
cout<<"输入学号:";
cin>>bb.no;
cout<<"输入语文:";
cin>>bb.yu;
cout<<"输入数学:";
cin>>bb.shu;
cout<<"输入英语:";
cin>>bb.ying;
cout<<i<<endl;*/

//}


dstudent();
}
YOU2005 2006-09-02
  • 打赏
  • 举报
回复
我是用VC的!!你的GETLINE行不通!!!请高手指导
YOU2005 2006-09-02
  • 打赏
  • 举报
回复
经过昨晚没睡,你看我这行得通吗?


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include <iostream.h>
#include <fstream.h>
#include <string.h>

struct chengji{
char name[30];
char no[30];
float yu;
float shu;
float ying;
};


void xstudent(chengji qq) //写入文件
{
fstream a;
a.open("d:\\abc.txt",ios::app);
a<<qq.yu<<endl;
a<<qq.shu<<endl;
a<<qq.ying<<endl;
a<<qq.name<<endl;
a<<qq.no<<endl;
a.close();

}

void dstudent(chengji bb) //读取文件
{
char ch;
fstream a;
a.open("d:\\abc.txt",ios::in);

do
{
a>>bb.yu;
a>>bb.shu;
a>>bb.ying;
a>>bb.name;
a>>bb.no;
cout<<"姓名"<<bb.name<<endl;
cout<<"学号"<<bb.no<<endl;
cout<<"语文"<<bb.yu<<endl;
cout<<"数学"<<bb.shu<<endl;
cout<<"英语"<<bb.ying<<endl;
cout<<endl;
}
while(a.get(ch)); //直到文件尾部

a.close();
}





void main()
{


chengji bb;

for (int i=0;i<3;i++)
{
cout<<"输入姓名:";
cin>>bb.name;
cout<<"输入学号:";
cin>>bb.no;
cout<<"输入语文:";
cin>>bb.yu;
cout<<"输入数学:";
cin>>bb.shu;
cout<<"输入英语:";
cin>>bb.ying;
cout<<i<<endl;
xstudent(bb);
}


dstudent(bb);



}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
xiantongyuan 2006-09-01
  • 打赏
  • 举报
回复
for循环少了一个括号;
toucher 2006-09-01
  • 打赏
  • 举报
回复
第2个问被你搞得太复杂了,重写好了,建议在第一个问加两个for循环应该就行了
toucher 2006-09-01
  • 打赏
  • 举报
回复
第一个问题在后面加:
a.open ("d:\\abc.txt",ios::in);
string str ;
while (getline (a, str))
{
cout << str << endl ;
}
YOU2005 2006-09-01
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <fstream.h>
#include <string.h>

struct chengji{
char name[12];
char no[2];
float chengj[3];
};


student(chengji qq)
{
fstream a;
a.open("d:\\abc.txt",ios::app);
a<<qq.chengj[0]<<endl;
a<<qq.chengj[1]<<endl;
a<<qq.chengj[2]<<endl;
a<<qq.name<<endl;
a<<qq.no<<endl;
a.close();
}

main()
{

char a,b;
float c,d,e;
chengji bb[10];
student(chengji qq);


for (int i=0;i<10;i++)
{
cout<<"输入语文";
cin>>bb.chengj[0];
cout<<"输入数学";
cin>>bb.chengj[1];
cout<<"输入英语";
cin>>bb.chengj[2];
cout<<"输入姓名";
cin>>bb.name;
cout<<"输入学号";
cin>>bb.no;
student(bb[i]);
}


错在那里!!!!请指教!!!!

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧