#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
using namespace std;
struct FishOi1
{
string name;
string uid;
char sex;
};
// bool InitFishC();
bool ReadFishC();
void RecordFishC();
int WriteFishC(FishOi1 * p);
int main()
{
int i;
// InitFishC();
while(1)
{
cout<<"请选择需要进行的操作:\n";
cout<<"1.打印数据\n";
cout<<"2.录入数据\n";
cout<<"3.退出程序\n";
cin>>i;
switch(i)
{
case 1:
if(ReadFishC())
cout<<"成功读取文件\n\n";
else
cout<<"读出文件失败\n\n";
break;
case 2:
RecordFishC();
break;
case 3:
return 0;
}
}
cout<<"初始化失败\n\n";
return 0;
}
// bool InitFishC()
// {
// }
bool ReadFishC()
{
ifstream ifile;
ifile.open("d:\\fishc.txt");
if(!ifile)
return false;
else{
cout<<"name"<<"\t"<<"uid"<<"\t"<<"sex"<<endl;
FishOi1 pIO;
while(ifile>>pIO.name){
ifile>>pIO.uid>>pIO.sex;
cout<<pIO.name<<"\t"<<pIO.uid<<"\t"<<pIO.sex<<endl;
}
ifile.close();
return true;
}
}
void RecordFishC()
{
ofstream ofile;
ofile.open("d:\\fishc.txt",ios::app);
if(!ofile){
cout<<"录入失败\n";
}
else{
int flag=1;
FishOi1 pIO;
while(flag){
flag=WriteFishC(&pIO);
ofile<<pIO.name<<' '<<pIO.uid<<' '<<pIO.sex<<'\n';
}
cout<<"录入完成\n";
ofile.close();
}
}
int WriteFishC(FishOi1 * p)
{
int b;
cout<<"name: ";
cin>>p->name;
cout<<"uid: ";
cin>>p->uid;
cout<<"sex: ";
cin>>p->sex;
cout<<"是否录入?1(yes) or 0(no):";
cin>>b;
return b;
}
之前没看过小甲鱼老师的c语言教程,不知道他的代码是怎么写的。
自己试了试,额。。。。都被我改得面目全非了。。。
但是里面的文件数据读取和保存还是实现了 |