将结构体写入为二进制文件(C++)

在struct中,我定义了int,unsigned char,string,double等类型。

开始时,我写入用来流fstream,(“”,ios::out||ios::binary);write方法对于char 及string 来说,可以实现写入,但是当写double时我遇到了问题,最终无法解决,因为write(×char,size_t),我无法将double->char*.

于是,采用了fwrite(&g,8,1,fout);可以实现

下面是代码:

#include<iostream>
#include<fstream>
#include"string"
using namespace std;
template <class T>//用来实现T to string(T代表类型),后边没用到

string ConvertToString(T value) {
    std::stringstream ss;
      ss<<value;
      
      return ss.str();
}
//enum fac_type{Airport=1,Road};
struct inf{
    unsigned char b_l;
    unsigned char b_ctry;
    unsigned char b_p;
    unsigned char b_r;
    unsigned char b_s;
    unsigned char b_cty;
    unsigned char b_ci;
    unsigned char b_t;
    unsigned char b_su;
    unsigned char b_sa;
    unsigned char b_o;
    unsigned char b_a;
    string locid;
    int type;
    string country;
    string postal;
    string region;
    string state;
    string county;
    string city;
    string town;
    string street_num;
    string street_nam;
    double lon;
    double lat;
    string offi_name;
    string alter_name;
};
void shuchu(inf ginf)
{
    cout<<ginf.locid<<","<<ginf.type<<","<<ginf.country<<","<<ginf.postal<<","<<ginf.region<<","<<ginf.state<<","<<ginf.county<<","<<ginf.city<<","<<ginf.town<<","<<ginf.street_num<<","<<ginf.street_nam<<","<<ginf.lon<<","<<ginf.lat<<","<<ginf.offi_name<<endl;
}
void fshuchu(inf ginf,FILE *fout)
{
    //string foutname;
    //cin>>foutname;
    /*fstream fout(foutname.c_str(),ios::out|ios::binary);*/
    fwrite(&ginf.b_l,1,1,fout);
    fwrite(ginf.locid.c_str(),ginf.locid.length(),1,fout);
    fwrite(&ginf.type,1,1,fout);//
    fwrite(ginf.country.c_str(),ginf.country.length(),1,fout);

    fwrite(&ginf.b_ctry,1,1,fout);
    fwrite(ginf.postal.c_str(),ginf.postal.length(),1,fout);
    
    fwrite(&ginf.b_p,1,1,fout);
    fwrite(ginf.region.c_str(),ginf.region.length(),1,fout);

    fwrite(&ginf.b_r,1,1,fout);
    fwrite(ginf.state.c_str(),ginf.state.length(),1,fout);
    
    fwrite(&ginf.b_s,1,1,fout);
    fwrite(ginf.county.c_str(),ginf.county.length(),1,fout);
    
    fwrite(&ginf.b_cty,1,1,fout);
    fwrite(ginf.city.c_str(),ginf.city.length(),1,fout);
    
    fwrite(&ginf.b_ci,1,1,fout);
    fwrite(ginf.town.c_str(),ginf.town.length(),1,fout);
    
    fwrite(&ginf.b_t,1,1,fout);
    fwrite(ginf.street_num.c_str(),ginf.street_num.length(),1,fout);
    
    fwrite(&ginf.b_su,1,1,fout);
    fwrite(&ginf.lon,8,1,fout);
    fwrite(&ginf.lat,8,1,fout);
    fwrite(ginf.offi_name.c_str(),ginf.offi_name.length(),1,fout);
    
    fwrite(&ginf.b_o,1,1,fout);
    fwrite(ginf.alter_name.c_str(),ginf.alter_name.length(),1,fout);
    
    fwrite(&ginf.b_a,1,1,fout);
    //inf *b=&ginf;
    //fout.write((char *)b,sizeof(ginf));
}

inf getinf(string s)
{
    inf ginf;
    string sg[15];
    for(int i=0,j=0;i<s.length();i++){
        if(s.at(i)!=',')
            sg[j]+=s.at(i);
        else
            j++;
    }
    ginf.locid=sg[0];
    ginf.b_l=(int)sg[0].length()&0xFF;//得到地位数放在char中,因为我用的int<10
    ginf.type=1;
    ginf.country=sg[2];
    ginf.b_ctry=(int)sg[2].length()&0xFF;
    ginf.postal=sg[3];
    ginf.b_p=(int)sg[3].length()&0xFF;
    ginf.region=sg[4];
    ginf.b_r=(int)sg[4].length()&0xFF;
    ginf.state=sg[5];
    ginf.b_s=(int)sg[5].length()&0xFF;
    ginf.county=sg[6];
    ginf.b_cty=(int)sg[6].length()&0xFF;
    ginf.city=sg[7];
    ginf.b_ci=(int)sg[7].length()&0xFF;
    ginf.town=sg[8];
    ginf.b_t=(int)sg[8].length()&0xFF;
    ginf.street_num=sg[9];
    ginf.b_su=(int)sg[9].length()&0xFF;
    ginf.street_nam=sg[10];
    ginf.b_sa=(int)sg[10].length()&0xFF;
    ginf.lon=atof(sg[11].c_str());
    ginf.lat=atof(sg[12].c_str());
    ginf.offi_name=sg[13];
    ginf.b_o=(int)sg[13].length()&0xFF;
    ginf.alter_name=sg[14];
    ginf.b_a=(int)sg[14].length()&0xFF;
    return ginf;
}
int main()
{
    string fname;
    cout<<"enter the file1 name";
    cin>>fname;
    string foutname;
    cout<<"enter the file2 name";
    cin>>foutname;
    ifstream fin(fname.c_str(),ios::in);
    if(!fin){cout<<""<<endl;}

    string str_g;
    FILE *fout;
    fout=fopen(foutname.c_str(),"rb++");
    while(getline(fin,str_g)){
        char ptr;
        ptr=str_g.at(0);
        if(ptr=='#')
            continue;
        else if(str_g.substr(0,5)=="Locid")
            continue;
        inf ginf=getinf(str_g);
        shuchu(ginf);
        fshuchu(ginf,fout);


    }
    fin.close();
    fclose(fout);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值