C++ iostream出现问题,首先以二进制模式写入文件,然后转换为ASCII并保存

C++ iostream出现问题,首先以二进制模式写入文件,然后转换为ASCII并保存,c++,iostream,C++,Iostream,我正在使用iostream从设备收集数据。首先,我以ASCII模式写入文件,它运行良好,但需要相当长的时间。然后,我切换为以二进制模式写入,速度更快 float dist[41616]; ofstream d_ofs( "numbers.dat",ios::out |ios::app|ios::binary); // this is from the device API getting distance datas res = GetDistances (hnd, dist, sizeof

我正在使用iostream从设备收集数据。首先,我以ASCII模式写入文件,它运行良好,但需要相当长的时间。然后,我切换为以二进制模式写入,速度更快

float dist[41616];
ofstream d_ofs( "numbers.dat",ios::out |ios::app|ios::binary);

// this is from the device API getting distance datas
 res = GetDistances (hnd, dist, sizeof dist);

d_ofs.write((char*)&dist,sizeof dist);
d_ofs.close();
在数据收集之后,我必须将“numbers.txt”中的数据转换成一个txt文件,以供我的导师阅读。我假设是将二进制原始数据转换为ASCII。所以我先读文件,然后用ASCII格式写

我得到了数字,但不幸的是,它们不是正确的数字。我想这个转换过程应该有特殊的格式。但到目前为止,我还没有找到解决方案,因为我的编码知识很差。有人能帮我一下吗?非常感谢

这是我的密码:

double fnum[41616]; // here is the mistake,it should be float[]
ifstream in("numbers.dat", ios::in | ios::binary);
in.read((char *) &fnum, sizeof fnum);
MessageBox("Read File done");
ofstream fout("output.txt");
for(int k=0; k<41616; k++) // show values read from file
    fout <<dec<<"\t" << fnum[k] ;
in.close();
fout.close();
double-fnum[41616];//这里是错误,它应该是float[]
ifstream-in(“numbers.dat”,ios::in | ios::binary);
in.read((char*)&fnum,fnum的大小);
MessageBox(“读取文件完成”);
流fout(“output.txt”);

对于(int k=0;k您似乎将保存为浮点,读取为双精度。读取(通常)将是写入的两倍,因此您读取的每一个双精度值实际上将读取两个写入的浮点。因此,您将得到错误的数字

试着换个颜色

double fnum[41616]

此外,最好不要调用二进制文件*.txt,因为它有点令人困惑。
调用双数组也是一件令人费解的事情,正如f所建议的浮动一样,确切地说,你是如何将这些数字写入numbers.txt的?请编辑我的帖子并提供更多信息。
float fnum[41616]