请问如何在vs2008下读取.bin文件,即二进制文件???
A.bin中的数据
000000:01CB 1AA9 9D0D 7CDC 4972 82F6 486C 64B4
000010:A08B 25A1 05CA DBE9 2F91 8E51 8623 4635
B.bin中的数据
000000:0149 DC7C 0D9D A91A CB48 F682 726C 64B4
000010:A08B 25A1 05CA DBE9 2F91 8E51 8623 4635
我如何把A.bin中的数据中的数据变为B.bin中的数据,
即A的红体数据倒叙为B中的红体数据,A中的蓝体数据倒叙为B中的蓝体数据。
我把我全部积分奉上了~
我的目的是写一个小程序,把A.bin中的数据转变顺序后生成B.bin的数据!
[解决办法]
直接fread读进来,然后一个字节一个字节的交换呗。。
[解决办法]
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
不要把
fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待
和
fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待
弄混了
[解决办法]
fopen的时候后边那个参数加"b"就行了
[解决办法]
下面代码直接读取A, 修改A的字符. 如果要写到B也一样.
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
void test()
{
ifstream in;
ofstream out;
in.open("c:\\A.bin",ios_base::in
[解决办法]
ios_base::binary);
out.open("c:\\A.bin",ios_base::out
[解决办法]
ios_base::binary);
in.seekg(1, ios::beg);
out.seekp(1, ios::beg);
unsigned char buf[12];
in.read((char*)buf, sizeof(buf));
if (in.fail()) return;
reverse(buf, buf+8);
reverse(buf+8, buf+12);
out.write((char*)buf, sizeof(buf));
if (out.fail()) return;
in.close();
out.close();
}
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test()
{
ifstream in;
ofstream out;
in.open("c:\\A.bin",ios_base::in
[解决办法]
ios_base::binary);
out.open("c:\\A.bin",ios_base::out
[解决办法]
ios_base::binary);
in.seekg(1, ios::beg);
out.seekp(1, ios::beg);
unsigned char buf[12];
in.read((char*)buf, sizeof(buf));
if (in.fail()) return;
reverse(buf, buf+8);
reverse(buf+8, buf+12);
out.write((char*)buf, sizeof(buf));
if (out.fail()) return;
in.close();
out.close();
}
#include<stdio.h>
int main()
{
FILE* fp;
if (fopen_s(&fp, "A.bin", "rb"))
return 0;
unsigned char *buf;
fseek(fp, 0, SEEK_END);
int len=ftell(fp);
buf= new unsigned char[len];
fseek(fp, 0, SEEK_SET);
for (int i=1; i<=len; i++)
fread(buf+len-i, 1, 1, fp);
fclose(fp);
if (fopen_s(&fp, "A.bin", "wb"))
return 0;
fwrite(buf, 1, len, fp);
fclose(fp);
return 0;
}