| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1069 人关注过本帖
标题:求助:文件输入输出
只看楼主 加入收藏
zh19951215
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-3-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
求助:文件输入输出
编译已通过,但是存取款写不进文件里,而且注册账号时也有问题,是打开文件的方式有问题吗?
谢谢了~
#include"iostream"
using namespace std;
#include"fstream"
#include"string.h"
int numbers=0;
class Customer
{
 public:
 long account;
 //char code[20];   //这样子读出来是乱码
 long code;
 char name[20];
 double balance;
 Customer(long a,long c,char* n,double b):account(a),balance(b),code(c){
     strcpy(name,n);
 }
 Customer(){
 }
 void Deposit(double m)
 {
   balance+=m;
 }
 int Draw(double m)
 {
     if(m>balance) return 0;
     else
     balance-=m;
     return 1;
 }
 void Display()
 {
     cout<<"账号:"<<account<<endl;
     cout<<"余额:"<<balance<<endl;
 }
};
int Validate_account(long a)
 {
     fstream in("Customer.dat",ios::in|ios::binary);
     Customer* p=new Customer(000,000,"000",0);
     while(in)
     {
         in.read((char*)p,sizeof(Customer));
         if(a==p->account)
         {
             in.close();
             return 0;
         }
         continue;
     }
     in.close();
     return 1;
 }
 int Validate_code(long c,Customer* p)
 {
  if(c==p->code)
  return 1;
  return 0;
 }
 int Write(Customer* p)
 {
    cout<<"账号:";
    cin>>p->account;
    int t;
    if(numbers==0)  t=1;
    else t=Validate_account(p->account);
    if(t)
    {
      cout<<"姓名:";
      cin>>p->name;
      cout<<"密码:";
      cin>>p->code;
      cout<<"确认密码:";
      //char* code1;     //为什么改成了char 数组就不行了呢?密码都验证不了
      long code1;
      cin>>code1;
      if(Validate_code(code1,p))
     {
      fstream out("Customer.dat",ios::app|ios::out|ios::binary); //为什么如果是Customer.dat读出来是乱码?
      out.write((char*)p,sizeof(Customer));
      out.close();
      return 1;
     }
     else
     {
         cout<<"确认密码有误,请重新注册!"<<endl;
         return 0;
     }
   }
    else cout<<"该账号已存在,请重新注册!"<<endl;
    return 0;
 }
 main()
{
    while(1)
    {
     cout<<"1.注册"<<endl;
     cout<<"2.登陆"<<endl;
     cout<<"0.退出"<<endl;
     int t1;
     cin>>t1;
     if(t1==1)
     {
        Customer* p=new Customer(000,000,"000",0);
        if(Write(p))
        {
          numbers++;
          cout<<"注册成功!"<<endl;
          cout<<"************************"<<endl;
        }
        delete p;
     }
     if(t1==2)
      {
        long a;
        cout<<"账号:";
        cin>>a;
        Customer* p1=new Customer(000,000,"000",0);
        fstream io("Customer.dat",ios::in|ios::out|ios::app|ios::binary);
        while(io)
        {
            io.read((char*)p1,sizeof(Customer));
            if(p1->account==a)
            break;
        }
        if(!io)
        {
         cout<<"该账号用户不存在!"<<endl;
        }
        else{
         cout<<"密码:";
         long c;
         cin>>c;
         if(Validate_code(c,p1))
         {
          while(1)
          {
          cout<<"1.存款"<<endl;
          cout<<"2.取款"<<endl;
          cout<<"3.查询余额"<<endl;
          cout<<"0.回到注册登录菜单"<<endl;
          int t2;
          cin>>t2;
          if(t2==1)
            {
                cout<<"请输入存款金额:";
                double b;
                cin>>b;
                p1->Deposit(b);
                io.seekp(-sizeof(Customer),ios::cur);
                io.write((char*) p1,sizeof(Customer));
                cout<<"交易成功!"<<endl;
                cout<<"**********************"<<endl;
            }
            if(t2==2)
            {
                cout<<"请输入取款金额:";
                io.seekg(-sizeof(Customer),ios::cur);
                io.read((char*) p1,sizeof(Customer));
                double b_temp;
                cin>>b_temp;
                if( p1->Draw(b_temp))
                {
                    io.seekp(-sizeof(Customer),ios::cur);
                    io.write((char*) p1,sizeof(Customer));
                  // io.seekg(-sizeof(Customer),ios::cur);
                   //io.read((char*) p1,sizeof(Customer));
                   cout<<"交易成功!"<<endl;
                   cout<<"**********************"<<endl;

                }
                else
                {
                   cout<<"余额不足!"<<endl;
                   cout<<"**********************"<<endl;
                }
            }
            if(t2==3)
            {
                   Customer* p2=new Customer(000,000,"000",0); //我是想检测一下文件里的内容是否改变了
                io.seekp(-sizeof(Customer),ios::cur);
                io.read((char*) p2,sizeof(Customer));
                p2->Display();
                delete p2;
             // p1->Display();
              cout<<"**********************"<<endl;
            }
            if(t2==0) break;
         }
          io.close();
        }
        else{cout<<"密码错误!"<<endl;}

    }
    }
    if(t1==0)  break;
}
}
搜索更多相关主题的帖子: account balance include numbers 注册账号 
2015-05-10 21:13
zcdjt
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:99
专家分:181
注 册:2014-9-9
收藏
得分:10 
你的头文件为什么这样写?看不懂

今朝醉
2015-05-10 21:40
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:10 
是不是要去掉ios::binary,我猜的
试一下      
        fstream temptfile;
    temptfile.open(".\\save\\save1.dat",ios::out);
    temptfile.write(code1,strlen(code1));
    temptfile.write("\n",1);
    temptfile.close();temptfile.clear();
看这样能否接受字符串
2015-05-11 07:42
zh19951215
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-3-16
收藏
得分:0 
回复 3楼 yangfrancis
如果去掉ios::binary实现更新文件内容会不会更麻烦了呢?ios::in和ios::out使用的是同一个文件游标,这样子好定位要修改的位置。
噢噢噢噢。。。clear(),我试试这个,谢谢啦~
2015-05-12 20:36
zh19951215
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-3-16
收藏
得分:0 
回复 2楼 zcdjt
你是说 using namespace std 吗?我是想导入整个名称空间的,比较懒就没有分开去写 std::cin,std::cout了
2015-05-12 20:38
tinmen
Rank: 1
来 自:广东
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-12-18
收藏
得分:0 
哇,什么情况,主函数还可以没有说明类型的吗?

我是新手
2015-12-18 21:35
快速回复:求助:文件输入输出
数据加载中...
 
   



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

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