In struct, I have defined types such as int,unsigned char,string,double.
At the beginning, I write to stream fstream, ("", ios::out| | ios::binary); The Write method can be written for char and string, but I have a problem when I write a double, and ultimately I can't fix it, because write (xchar,size_t), I can't double->char* .
Thus, the fwrite (&g,8,1,fout) is used;
Here's the code:
#include <iostream>
#include <fstream>
#include "string"
using namespace Std;
Template <class t>//is used to implement T to string (t represents type)
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;//Gets the position number in char because I used the 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;
}