把二进制文件转换为文本格式(十进制)

首先要清楚文件里, 储存的数据的类型,是int,long,long long 还是 float,double
其次文件有无字节序(大小端)问题
这两个问题解决了
直接按照数据类型定义一个定长数据,或者数组一次性读入,或者分批读入全部文件。
需要的话,读取以后,先转换一下字节顺序
fopen,二进制读方式,打开文件,fread 读取文件,fclose 关闭文件

然后 转换成十进制 ACII格式的数据,输出到文本文件中去。
 fopen,文本写方式,打开文件,fprintf 写入文件,fclose 关闭文件。

基本上就可以了

具体可以了解一下 fopen,fread,fwrie,fclose , fprintf ,fscanf  
这些C流式文件读写,打开,关闭函数

这些都是C标准库的函数,使用的时候 #include <stdio.h> 就可以了


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.

示例代码:读入二进制文件再写到文本中去

在这个例子中,二进制文本大概长这样:F1 D3 35 C4 35 38 3E ...

它实质上记录的是float型的数据(float占4个字节,即每4个字节表示一个十进制里的float数据)。每个float数据其实是一个三维坐标点的一个坐标值。在下面的代码中,每次连续读入三个float值从而得到一个坐标点。主要用到函数是:

istream& read(char* buffer, int count);

这个函数表示从文件流中读数据,读多少呢?读取count个字节的数据;存到哪呢?存到参数buffer指向的内存位置。注意这个内存位置由一个字符指针表示(上面的第一个参数),在必要的时候需要强制类型转换。一般我们先定义一个变量用于保存,比如在下面代码中该参数为&coordinates[i]。每完成一次读操作,文件读指针就往后移动相应的字节。

代码:

#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++){
         infile.read((char*)&coordinates[i], sizeof(float));
         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;
}

这里稍微解释一下infile.read((char*)&coordinates[i], sizeof(float));:

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

下面这个链接用一个简单例子说明了c++二进制文件读取和写出的用法

c.biancheng.net/view/302.html

猜你喜欢

转载自blog.csdn.net/qq_41230365/article/details/88124002