c++文件读写

huasonl88 2009-05-07 06:21:21
我建了一个test文档,里面存有
struct stu
{
char name[20];
char id[10];
float score;
};的若干条记录(假设不确定有几条)
假设文件已有如下记录:
zhao 001 89
li 002 88
ping 003 23
dsf 004 67
hong 005 56
sf 006 78

然后我用下面代码,读入内存:
#include <iostream.h>
#include <fstream.h>
struct stu
{
char name[20];
char id[10];
float score;
};
void main()
{
ifstream cin1;
cin1.open("input.txt");
char ch;
cin1.get(ch);
while(cin1.good())
{
cout<<ch;
cin1.get(ch);
}
cin1.close();
}
这样的话,在屏幕上会把文件里的所有内容都显示在屏幕上,但现在要求只显示分数低于六十的记录(给定一个条件),在不知道记录有几条的情况下,可以实现吗?能给出代码吗?也就是我要对文件进行直接操作,而不要通过结构体数组来实现(这样太死板了)。再者,能不能实现对里面记录按分数高低再重新写入到文件中? 能给出代码那是最好不过了~ 谢谢了~~~
...全文
258 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizhaohu 2009-05-07
  • 打赏
  • 举报
回复

// 1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
#include <set>
#include <fstream>
#include <functional>
using namespace std;
struct student
{
char name[20];
char id[10];
float score;
};
class less_than:public binary_function<student,student,bool>
{
private:

public:
bool operator()(const student& lhs,const student& rhs) const
{
return lhs.score<rhs.score;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile("c:\\input.txt");
if(!infile)
{
cout<<"Open file is failure!"<<endl;
exit(-1);
}
set<student,less_than> sample;
student test;
while(!infile.fail())
{
infile>>test.name>>test.id>>test.score;
sample.insert(test);
}
set<student,less_than>::iterator it=sample.begin();
for(;it!=sample.end();++it)
{
if((*it).score<60)

cout<<(*it).name<<" "<<(*it).id<<" "<<(*it).score<<endl;
}


return 0;
}
lzh9955 2009-05-07
  • 打赏
  • 举报
回复
学习!!!
zxianrong 2009-05-07
  • 打赏
  • 举报
回复
LS的OK 了
tangshuiling 2009-05-07
  • 打赏
  • 举报
回复

上面的没按要求,稍微改动了一下
#include <iostream>
#include <set>
#include <fstream>
#include <functional>
using namespace std;
struct student
{
char name[20];
char id[10];
float score;
};
class less_than:public binary_function<student,student,bool>
{
private:

public:
bool operator()(const student& lhs,const student& rhs) const
{
return lhs.score<rhs.score;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile("c:\\input.txt");
if(!infile)
{
cout<<"Open file is failure!"<<endl;
exit(-1);
}
set<student,less_than> sample;
student test;
while(!infile.fail())
{
infile>>test.name>>test.id>>test.score;
sample.insert(test);
}
set<student,less_than>::iterator it=sample.begin();
ofstream outfile("c:\\outfile.txt");
for(;it!=sample.end();++it)
{
outfile<<(*it).name<<" "<<(*it).id<<" "<<(*it).score<<endl;
if((*it).score>60)
cout<<(*it).name<<" "<<(*it).id<<" "<<(*it).score<<endl;
}
return 0;
}
tangshuiling 2009-05-07
  • 打赏
  • 举报
回复

随意写了一个,楼主参考一下:
#include <iostream>
#include <set>
#include <fstream>
#include <functional>
using namespace std;
struct student
{
char name[20];
char id[10];
float score;
};
class less_than:public binary_function<student,student,bool>
{
private:

public:
bool operator()(const student& lhs,const student& rhs) const
{
return lhs.score<rhs.score;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile("c:\\input.txt");
if(!infile)
{
cout<<"Open file is failure!"<<endl;
exit(-1);
}
set<student,less_than> sample;
student test;
while(!infile.fail())
{
infile>>test.name>>test.id>>test.score;
sample.insert(test);
}
set<student,less_than>::iterator it=sample.begin();
for(;it!=sample.end();++it)
cout<<(*it).name<<" "<<(*it).id<<" "<<(*it).score<<endl;

return 0;
}

fairchild811 2009-05-07
  • 打赏
  • 举报
回复
判断,如果struct内的分数小于60.再显示,否则就不显示
  • 打赏
  • 举报
回复
这样的话,在屏幕上会把文件里的所有内容都显示在屏幕上,但现在要求只显示分数低于六十的记录(给定一个条件),在不知道记录有几条的情况下,可以实现吗?能给出代码吗?也就是我要对文件进行直接操作,而不要通过结构体数组来实现(这样太死板了)。再者,能不能实现对里面记录按分数高低再重新写入到文件中? 能给出代码那是最好不过了~ 谢谢了~~~

你判断一下参数的分数,再决定是否打印到屏幕啊。


zhao 001 89
li 002 88
ping 003 23
dsf 004 67
hong 005 56
sf 006 78


这些数据你可以fscanf来读取啊。

FIEL* fd;
....

while(..)
fscanf(fd,"%s %d %d\n",name,&num,&sccore) //这样读就可以了啊
mengde007 2009-05-07
  • 打赏
  • 举报
回复
怎么这么多条件;本来准备给你写一个;但是条件太多,算了吧;
blingpro 2009-05-07
  • 打赏
  • 举报
回复
string strInfo;
vector<stu>vecStu;
while(..)
{
cin>>strInfo;
praseInfo(&stu,strInfo); //解析一下结构体
vecStu.push_back(stu);
}
sort(stu);
//
参考
herman~~ 2009-05-07
  • 打赏
  • 举报
回复
mark
yuyunliuhen 2009-05-07
  • 打赏
  • 举报
回复
飘过。。。

65,211

社区成员

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

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