把二进制文件转换为文本格式（十进制）

fopen,文本写方式，打开文件，fprintf 写入文件，fclose 关闭文件。

C++ 可以用C++流 std::fstream ，std::ifstream,std::ofstream 做同样的事情。

#include <fstream>

What exactly causes binary file “gibberish”?

I haven't found an answer to this particular question; perhaps there isn't one. But I've been wondering for a while about it.

What exactly causes a binary file to display as "gibberish" when you look at it in a text editor? It's the same thing with encrypted files. Are the binary values of the file trying to be converted into ASCII? Is it possible to convert the view to display raw binary values, i.e. to show the 1s and 0s that make up the file?

Finally, is there a way to determine what program will properly open a data file? Many times, especially with Windows, a file is orphaned or otherwise not associated w/ a particular program. Opening it in a text editor sometimes tells you where it belongs but most of the time doesn't, due to the gibberish. If the extension doesn't provide any information, how can you determine what program it belongs to?

• Are the binary values of the file trying to be converted into ASCII?

Yes, that's exactly what's happening. Typically, the binary values of the file also include ASCII control characters that aren't printable, resulting in even more bizarre display in a typical text editor.

• Is it possible to convert the view to display raw binary values, i.e. to show the 1s and 0s that make up the file?

It depends on your editor. What you want is a "hex editor", rather than a normal text editor. This will show you the raw contents of the file (typically in hexadecimal rather than binary, since the zeros and ones would take up a lot of space and be harder to read).

• Finally, is there a way to determine what program will properly open a data file?

There is a Linux command-line program called "file" that will attempt to analyze the file (typically looking for common header patterns) and tell you what sort of file it is (for example text, or audio, or video, or XML, etc). I'm not sure if there is an equivalent program for Windows. Of course, the output of this program is just a guess, but it can be very useful when you don't know what the format of a file is.

#include <fstream>
#include <iostream>
using namespace std;

int main(){
//把一个数据点的三个坐标放到一个float数组中
const int num_of_coord = 3;
float coordinates[num_of_coord]={0.};

const char* InFileName = "data.log";
const char* OutFileName = "data.txt";

//用构造函数创建文件流对象，以二进制方式读入，以文本方式写出
ifstream infile(InFileName, ios::binary);
ofstream outfile(OutFileName);

if(!(infile && outfile)){
cout<<"open file error"<<endl;
return -1;
}

while(!infile.eof()){
//每次循环读入三个坐标值，每个坐标值对应的二进制数据长度其实就是float的字节数
for(int i=0; i<num_of_coord; i++){
cout<<coordinates[i]<<", ";
}
cout<<endl;

for(int i=0; i<num_of_coord; i++)
outfile<<coordinates[i]<<", ";
outfile<<endl;  //每输出一个点的三个坐标值，换行一次
}

infile.close();
outfile.close();
return 0;
}


coordinates[i]实际上就是个float类型，&coordinates[i]就是取它的内存地址，换句话说是指向float（4个字节）的类型指针。(char*)&coordinates[i]是加上强制类型转换，将该地址由float*转换为char*，指针类型转换为指向一个字节的字符指针。想想这是为什么？

c.biancheng.net/view/302.html

• 0
点赞
• 0
评论
• 6
收藏
• 打赏
• 扫一扫，分享海报

08-29
06-06 1万+

05-03
09-29
06-20 2万+
12-05
04-27 879
04-21
01-12
01-08 2290
04-28 3882
07-17 461

¥2 ¥4 ¥6 ¥10 ¥20

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