求助,关于文件输出输入流
数据输入到文件里怎么都是一些乱码啊,困扰了好久啊
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
static int n=0;
class Time
{
protected:
int year,month;
public:
inline void print()
{
cout<<"订单的时间:"<<setw(4)<<year<<"-"<<setw(2)<<setfill('0')<<month<<endl;
}
}; //客户订单管理系统基本属性及些基本操作
class customer_Information:public Time
{
public:
void getdata();
void show();
void compair(string a);
int delet(string a);
protected:
char name[10];
char sex[5];
char sid[9];
};
void customer_Information::getdata()
{
cout<<"输入客户姓名:";cin>>name;
cout<<"客户的性别:";cin>>sex;
cout<<"订单的时间(年):";cin>>year;
cout<<"订单的时间(月):";cin>>month;
cout<<endl;
}
void customer_Information::show()
{
cout<<"姓名:"<<name<<endl;
cout<<"性别:"<<sex<<endl;
}
void customer_Information::compair(string a)
{
int j=0;
for(int k=0;k<a.length();k++)
{
if(name[k]==a[k])
{
j++;
}
};
if(j==a.length())
{
show();
print();
}
}
int customer_Information::delet(string a)
{
int j=0;
string f="have del";
for(int k=0;k<a.length();k++)
{
if(name[k]==a[k])
{
j++;
}
}
if(j==a.length())
{
for(int m=0;m<10;m++)
{
name[m]=f[m];
sex[m]=f[m];
sid[m]=f[m];
}
year=0;
month=0;
return 1;
}
else return 2;
} //功能界面的功能类
class Fun
{
public:
Fun()
{
fstream file("file.txt");
if(!file)
{
cout<<"file doesn't exist,please set it."<<endl;
exit(1);
}
file.close();
}
void fun()
{
char m;
bool flag=false;
Loop:
cout<<" 1:录入信息 "<<endl;
cout<<" 2:修改信息 "<<endl;
cout<<" 3:显示信息 "<<endl;
cout<<" 4:查找信息 "<<endl;
cout<<" 5:删除信息 "<<endl;
cout<<" 退出请输入其他任意字符 "<<endl;
cout<<" 请输入要操作序号(输入 1/2/3/4/5)"<<endl;
cin>>m;
switch(m)
{
case '1':input();break;
case '2':correct();break;
case '3':output();break;
case '4':find();break;
case '5':del();break;
default:flag=true;
}
if(flag=false) goto Loop;
}
void input();
void output();
void correct();
void find();
void del();
};
void Fun::input()
{
char l;
do
{
customer_Information A;
A.getdata();
fstream outfile("file.txt",ios::binary|ios::app|ios::in|ios::out);
outfile.write((char*)&A,sizeof(A));
outfile.close();
cout<<"按0继续录入,其他键返回上级菜单!"<<endl;
cin>>l;
}while(l=='0');
fun();
}
void Fun::output()
{
char l;
do{
int n=0;
customer_Information B;
fstream input("file.txt",ios::binary|ios::in|ios::out);
while(!input.eof()) //eof()为真时,则表示遇到了文件结束符,循环则停止
{
input.seekg(n*sizeof(B)); //将输入文件中的指针移到指定位置
input.read((char*)&B,sizeof(B));
cout<<n+1<<":";
B.show();
B.print();
n++;
}
input.close();
cout<<"按0返回上一单元!"<<endl;
cin>>l;
}while( l ='0');
fun();
}
void Fun::correct()
{
int m=0,l=0;
while(l==0)
{
cout<<"要改第几条请输入:";
cin>>m;
customer_Information C;
C.getdata();
fstream output("file.txt",ios::binary|ios::in|ios::out);
output.seekp(sizeof(C)*(m-1),ios::beg); //定好位于第m条信息
output.write((char*)&C,sizeof(C));
output.close();
cout<<"按0继续修改,按其它键返回上一单元!"<<endl;
cin>>l;
}
fun();
}
void Fun::find()
{
int p=0;
string t;
char l='0';
do
{
customer_Information D;
cout<<"输入要查找的客户姓名:"<<endl;
cin>>t;
fstream output("file.txt",ios::binary|ios::in|ios::out);
while(!output.eof())
{
output.seekg(sizeof(D)*p);
output.read((char*)&D,sizeof(D));
(t);
p++;
}
cout<<"按0继续查找,其他键返回上一单元!"<<endl;
cin>>l;
}
while(l=='0');
fun();
}
void Fun::del()
{
int q=0,l=0;
string s;
while(l==0)
{
cout<<"请输入要删除的客户姓名:";
cin>>s;
customer_Information E;
fstream output("file.txt",ios::binary|ios::in|ios::out);
while(!output.eof())
{
output.seekg(sizeof(E)*q);
output.read((char*)&E,sizeof(E)); //读出这条订单的信息
if(E.delet(s)==1) //若删除成功
{
output.close();
fstream input("file.txt",ios::binary|ios::in|ios::out);
input.seekp(sizeof(E)*q);
input.write((char*)&E,sizeof(E));
}
q++;
}
cout<<"按0继续删除,按其它键返回上一单元!"<<endl;
cin>>l;
}
fun();
}
void main()
{
cout<<" 订单管理系统 "<<endl;
Fun a;
a.fun();
cout<<" 现有的订单的信息已经修改完善!"<<endl;
}
不知道这个程序哪里出问题了,各位能告诉我这个菜鸟吗