| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 320 人关注过本帖
标题:求助,关于文件输出输入流
只看楼主 加入收藏
空即是色
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-9-15
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
求助,关于文件输出输入流
数据输入到文件里怎么都是一些乱码啊,困扰了好久啊
搜索更多相关主题的帖子: 数据 
2011-05-19 11:55
空即是色
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-9-15
收藏
得分:0 
#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;
}
不知道这个程序哪里出问题了,各位能告诉我这个菜鸟吗
2011-05-19 12:02
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:10 
调试?OK?
2011-05-19 12:38
空即是色
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-9-15
收藏
得分:0 
回复 3楼 zhoufeng1988
不行啊,有好多错误,比如如果只有一个订单信息,它能显示出两个来
2011-05-19 13:21
记叙、继续
Rank: 4
等 级:业余侠客
帖 子:56
专家分:226
注 册:2011-5-17
收藏
得分:10 
不懂,但帮忙顶
2011-05-22 23:31
快速回复:求助,关于文件输出输入流
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012561 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved