我需要读取二进制文件并将数据保存在文本文件中。你能救我吗? [英] i need to read the binary file and save the data in text file. can u help me out?
本文介绍了我需要读取二进制文件并将数据保存在文本文件中。你能救我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我迫切需要它。我有二进制文件,我必须打开文件,读取它并将其数据转换为文本文件。我需要的数据应该是十进制格式。
解决方案
这并不困难。例如,使用快速和脏方法,二进制文件的内容(假定包含32位整数)以标准输出的十进制表示形式报告。
nclude< iostream>
#include < fstream >
#include < stdint.h >
使用 命名空间标准;
union MU
{
char buf [ 4 ];
uint32_t u32;
};
int main( int argc, char * argv [])
{
MU mu;
if (argc< 2 )返回 - 1 ;
ifstream ifs(argv [ 1 ],ios :: binary);
while (ifs.good())
{
if (ifs.read(mu.buf, sizeof (mu))。good())
{
cout<< ; mu.u32<< ENDL;
}
}
}
i need it urgently. I have binary files and i have to open the file, read it and convert its data to the text file. the data i require should be in decimal format.
解决方案
It is not difficult. For instance with the 'quick and dirty' approach the content of a binary file (assumed to contain 32 bit integers) is reported in decimal representation on the standard output.
nclude <iostream> #include <fstream> #include <stdint.h> using namespace std; union MU { char buf[4]; uint32_t u32; }; int main( int argc, char * argv[]) { MU mu; if ( argc < 2) return -1; ifstream ifs(argv[1], ios::binary); while (ifs.good()) { if ( ifs.read(mu.buf, sizeof(mu)).good()) { cout << mu.u32 << endl; } } }
这篇关于我需要读取二进制文件并将数据保存在文本文件中。你能救我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文