把⼆进制⽂件转换为⽂本格式(⼗进制)
⾸先要清楚⽂件⾥, 储存的数据的类型,是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>
为什么⽤笔记本打开⼆进制⽂件会出现乱码现象?
问题:
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 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数组中
fprintf格式const int num_of_coord = 3;
float coordinates[num_of_coord]={0.};
const char* InFileName = "data.log";
const char* OutFileName = "";
/
/⽤构造函数创建⽂件流对象,以⼆进制⽅式读⼊,以⽂本⽅式写出
ifstream infile(InFileName, ios::binary);
ofstream outfile(OutFileName);
if(!(infile && outfile)){
cout<<"open file error"<<endl;
return -1;
}
while(!f()){
//每次循环读⼊三个坐标值,每个坐标值对应的⼆进制数据长度其实就是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;
}
这⾥稍微解释⼀下ad((char*)&coordinates[i], sizeof(float));:
coordinates[i]实际上就是个float类型,&coordinates[i]就是取它的内存地址,换句话说是指向float(4个字节)的类型指针。
(char*)&coordinates[i]是加上强制类型转换,将该地址由float*转换为char*,指针类型转换为指向⼀个字节的字符指针。想想这是为什么?
下⾯这个链接⽤⼀个简单例⼦说明了c++⼆进制⽂件读取和写出的⽤法
c.biancheng/view/302.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论