请教高手:关于文件操作
时间:2008-06-04 18:30:09
来源:论坛整理 作者: 编辑:chinaitzhe
不知道为什么~
还有教材讲打开文件会返回数值,而编译的时候报错:error C2120: 'void' illegal with all types 怎么改?
谢谢~
//教材《C 面向对象程序设计》清华大学出版社 谭浩强编著 第八章 习题3
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const unsigned int N=7;//职员个数
struct employee//定义employee结构体
{
int num;
string name;
int age;
float wage;
};
employee emp[N];
int i;
int order=4;
ifstream ifile;//定义输入文件流ifile
ofstream ofile;//定义输出文件流ofile
void input()
{
employee temple;
cout < <"input information of employee:" < <endl;//输入T个职员的资料
cout < <"number:";
cin>>temple.num;
cout < <"name:";
cin>>temple.name;
cout < <"age:";
cin>>temple.age;
cout < <"wage:";
cin>>temple.wage;
cout < <endl;
ofile < <temple.num < <" " < <temple.name < <" " < <temple.age < <" " < <temple.wage < <endl;
}
void outputall(struct employee *e)
{for(i=0;i <N;i )//输出全部职员的资料
{
ifile>>e->num;
cout < <"number:" < <e->num < <" ";
ifile>>e->name;
cout < <"name:" < <e->name < <" ";
ifile>>e->age;
cout < <"age:" < <e->age < <" ";
ifile>>e->wage;
cout < <"wage:" < <e->wage < <endl;
e ;
}
cout < <endl;
}
void check(struct employee *e)
{
int num;
cout < <"input the employee's number:" < <endl;//输入职员号码(num)输出职员资料
cin>>num;
for(i=0;i <N;i )
{
if(e->num==num)
{
ifile>>e->num;
cout < <"number:" < <e->num < <" ";
ifile>>e->name;
cout < <"name:" < <e->name < <" ";
ifile>>e->age;
cout < <"age:" < <e->age < <" ";
ifile>>e->wage;
cout < <"wage:" < <e->wage < <endl;
goto out;
}
e ;
}
cerr < <"sorry! No." < <num < <" do not exist." < <endl;
out: ;
}
int main()
{
ifile.open("employee.txt",ios::in);
if(ifile.open("employee.txt",ios::in)==0)
{cerr < <"open employee.txt error!" < <endl;
exit(1);}
ofile.open("employee.txt",ios::out ¦ios::app);
if(ofile.open("employee.txt",ios::out ¦ios::app)==0)
{cerr < <"open employee.txt error!" < <endl;
exit(1);}
cout < <"welcome to the information of employee management system" < <endl;
cout < <"press 1 input information of employee" < <endl;
cout < <"press 2 look up the employee through the number" < <endl;
cout < <"press 3 output all tinformation" < <endl;
cout < <"press 0 quit" < <endl;
while(order!=0)
{
cout < <"order:";
cin>>order;
switch(order)
{
case 1: input(); break;
case 2: check(emp); break;
case 3: outputall(emp); break;
case 0: goto quit; break;
default: cerr < <"error!" < <endl;
}
}
quit:
ifile.close();
ofile.close();
return 0;
}
文件employee.txt
1 Adam 19 2000
2 Boa 19 5000
3 Cola 18 3000
4 David 23 2000
5 Eva 18 4000
网友回复:error C2120: 'void' illegal with all types
VC6.0中提示返回值为void
改为
if(!ifile.is_open())
{cerr < <"open employee.txt error!" < <endl;
exit(1);}
网友回复:ifile.open("employee.txt",ios::in);
if(ifile.open("employee.txt",ios::in)==0)
{cerr < <"open employee.txt error!" < <endl;
exit(1);}
ofile.open("employee.txt",ios::out |ios::app);
if(ofile.open("employee.txt",ios::out |ios::app)==0)
{cerr < <"open employee.txt error!" < <endl;
exit(1);}
这一段你要做什么啊?很希奇的写法。
fstream对象可以同时读写
这样的写法有问题
网友回复:帮你改了下:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <iostream> #include <fstream> #include <string> using namespace std; const unsigned int N=7;//职员个数 struct employee//定义employee结构体 { int num; string name; int age; float wage; }; employee emp[N]; int i; int order=4; ifstream ifile;//定义输入文件流ifile ofstream ofile;//定义输出文件流ofile void input() { employee temple; //ofstream ofile; cout <<"input information of employee:" <<endl;//输入T个职员的资料 cout <<"number:"; cin>>temple.num; cout <<"name:"; cin>>temple.name; cout <<"age:"; cin>>temple.age; cout <<"wage:"; cin>>temple.wage; cout <<endl; ofile <<temple.num <<" " <<temple.name <<" " <<temple.age <<" " <<temple.wage <<endl; } void outputall(struct employee *e) { //ifstream ifile; for(i=0;i <N;i )//输出全部职员的资料 { ifile>>e->num; cout <<"number:" <<e->num <<" "; ifile>>e->name; cout <<"name:" <<e->name <<" "; ifile>>e->age; cout <<"age:" <<e->age <<" "; ifile>>e->wage; cout <<"wage:" <<e->wage <<endl; e ; } cout <<endl; } void check(struct employee *e) { int num; //ifstream ifile; cout <<"input the employee's number:" <<endl;//输入职员号码(num)输出职员资料 cin>>num; for(i=0;i <N;i ) { if(e->num==num) { ifile>>e->num; cout <<"number:" <<e->num <<" "; ifile>>e->name; cout <<"name:" <<e->name <<" "; ifile>>e->age; cout <<"age:" <<e->age <<" "; ifile>>e->wage; cout <<"wage:" <<e->wage <<endl; goto out; } e ; } cerr <<"sorry! No." <<num <<" do not exist." <<endl; out: ; } int main() { //ifile.open("employee.txt",ios::in); //ifstream ifile; //ofstream ofile; ifile.open("employee.txt"); if(!ifile.is_open()) { //cerr <<"open employee.txt error!" <<endl; cout<< "Could not open the file \n"<< endl; exit(1); } ofile.open("employee.txt",ios::out|ios::app); cout <<"welcome to the information of employee management system" <<endl; cout <<"press 1 input information of employee" <<endl; cout <<"press 2 look up the employee through the number" <<endl; cout <<"press 3 output all tinformation" <<endl; cout <<"press 0 quit" <<endl; while(order!=0) { cout <<"order:"; cin>>order; switch(order) { case 1: input(); break; case 2: check(emp); break; case 3: outputall(emp); break; case 0: goto quit; break; default: cerr <<"error!" <<endl; } } quit: ifile.close(); ofile.close(); system("pause"); return 0; }
网友回复:最后一个小问题
在order:2的时候,输入职员号,输出职员信息,name:后面是空白的
是什么原因呢?是因为string不是内置类型?还是...
关键字:请教,高手,文件,操作,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面