C++写入二进制文件出现乱码
输出的全是乱码,不知原因,求教大神。#include<iostream>#include<fstream>usingnamespacestd;voidsort(doubl...
输出的全是乱码,不知原因,求教大神。
#include<iostream>
#include<fstream>
using namespace std;
void sort(double a[],int n)
{double min;int t;
for(int k=0;k<n-1;k++)
{t=k;
for(int l=k+1;l<n;l++)
if(a[t]>a[l]) t=l;
if(t!=k)
{min=a[k];
a[k]=a[t];
a[t]=min;}}
return;}
void main()
{fstream f,g,h;
double a[5]={1,2,6,7,9};double b[4]={3,5,8,10};double c[9];
f.open("d:\\data1.dat",ios::out|ios::binary);
g.open("d:\\data2.dat",ios::out|ios::binary);
int i, j;
for(i=0;i<5;i++)
{f.write((char*)&(a[i]),sizeof(double));}
for(j=0;j<4;j++)
{g.write((char*)&(b[j]),sizeof(double));}
f.seekg(0);
g.seekg(0);
for(i=0;i<5;i++)
{f.seekg(long(sizeof(double)*i));
f.read((char*)&(c[i]),sizeof(double));}
for(j=0;j<4;j++)
{ g.seekg(long(sizeof(double)*j));
g.read((char*)&(c[j+5]),sizeof(double));}
cout<<"before sorting:";
for(i=0;i<9;i++)
{cout<<c[i]<<" ";}
sort(c,9);
cout<<"after sorting:";
for(i=0;i<9;i++)
{cout<<c[i]<<" ";}
h.open("d:\\data3.dat",ios::out|ios::binary);
for(i=0;i<9;i++)
{h.write((char*)&c[i],sizeof(double));}
if(h)
{cout<<"File has been constructed."<<endl;}
f.close();
g.close();
h.close();
system("pause");
} 展开
#include<iostream>
#include<fstream>
using namespace std;
void sort(double a[],int n)
{double min;int t;
for(int k=0;k<n-1;k++)
{t=k;
for(int l=k+1;l<n;l++)
if(a[t]>a[l]) t=l;
if(t!=k)
{min=a[k];
a[k]=a[t];
a[t]=min;}}
return;}
void main()
{fstream f,g,h;
double a[5]={1,2,6,7,9};double b[4]={3,5,8,10};double c[9];
f.open("d:\\data1.dat",ios::out|ios::binary);
g.open("d:\\data2.dat",ios::out|ios::binary);
int i, j;
for(i=0;i<5;i++)
{f.write((char*)&(a[i]),sizeof(double));}
for(j=0;j<4;j++)
{g.write((char*)&(b[j]),sizeof(double));}
f.seekg(0);
g.seekg(0);
for(i=0;i<5;i++)
{f.seekg(long(sizeof(double)*i));
f.read((char*)&(c[i]),sizeof(double));}
for(j=0;j<4;j++)
{ g.seekg(long(sizeof(double)*j));
g.read((char*)&(c[j+5]),sizeof(double));}
cout<<"before sorting:";
for(i=0;i<9;i++)
{cout<<c[i]<<" ";}
sort(c,9);
cout<<"after sorting:";
for(i=0;i<9;i++)
{cout<<c[i]<<" ";}
h.open("d:\\data3.dat",ios::out|ios::binary);
for(i=0;i<9;i++)
{h.write((char*)&c[i],sizeof(double));}
if(h)
{cout<<"File has been constructed."<<endl;}
f.close();
g.close();
h.close();
system("pause");
} 展开
1个回答
展开全部
这个也采纳吧。。。。答题也不容易
#include<iostream>
#include<fstream>
using namespace std;
void sort(double a[],int n)
{double min;int t;
for(int k=0;k<n-1;k++)
{t=k;
for(int l=k+1;l<n;l++)
if(a[t]>a[l]) t=l;
if(t!=k)
{min=a[k];
a[k]=a[t];
a[t]=min;}}
return;}
void main()
{fstream f,g,h;
double a[5]={1,2,6,7,9};double b[4]={3,5,8,10};double c[9];
f.open("d:\\data1.dat",ios::out|ios::binary | ios::in); //你要定义读写流啊,下同
g.open("d:\\data2.dat",ios::out|ios::binary | ios::in);
int i, j;
for(i=0;i<5;i++)
{f.write((char*)&(a[i]),sizeof(double));}
for(j=0;j<4;j++)
{g.write((char*)&(b[j]),sizeof(double));}
f.seekg(0);
g.seekg(0);
for(i=0;i<5;i++)
{f.seekg(long(sizeof(double)*i));
f.read((char*)&(c[i]),sizeof(double));}
for(j=0;j<4;j++)
{ g.seekg(long(sizeof(double)*j));
g.read((char*)&(c[j+5]),sizeof(double));}
cout<<"before sorting:";
for(i=0;i<9;i++)
{cout<<c[i]<<" ";}
cout<<endl;
sort(c,9);
cout<<"after sorting:";
for(i=0;i<9;i++)
{cout<<c[i]<<" ";}
cout<<endl;
h.open("d:\\data3.dat",ios::out|ios::binary);
for(i=0;i<9;i++)
{h.write((char*)&c[i],sizeof(double));}
if(!h.eof())
{cout<<"File has been constructed."<<endl;}
f.close();
g.close();
h.close();
system("pause");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询