나는 우분투 10.04와 gcc에서 일하고있다. 나 자신의 마법 번호로 이진 파일을 가지고있다. 파일을 읽을 때 매직 번호가 같지 않습니다. 스트림 솔기가 맞아야합니다.표준 : : fstream 다른 크기로 읽을 것 같다
std::fstream chfile;
chfile.open(filename.c_str(), std::fstream::binary | std::fstream::in);
if (chfile.good())
{
unsigned char a,b,c,d;
chfile >> a;
chfile >> b;
chfile >> c;
chfile >> d;
printlnn("header must : " << (int)0x02 << ' ' << (int)0x46 << ' ' << (int)0x8A << ' ' << (int)0xCE); // macro for debugging output
printlnn("header read : " << (int)a << ' ' << (int)b << ' ' << (int)c << ' ' << (int)d);
chfile.close();
}
내가 그것을 괜찮아요 매직 넘버로 02 (46) 8A CE를 사용하는 경우 (출력이 말한대로) :
header must : 2 70 138 206
header read : 2 70 138 206
매직 넘버 읽기
std::fstream chfile;
chfile.open(filename.c_str(), std::fstream::binary | std::fstream::out);
if (chfile.good())
{
chfile << (unsigned char)0x02 << (unsigned char)0x46 << (unsigned char)0x8A << (unsigned char)0xCE;
// other input
chfile.close();
}
:
매직 넘버를 작성
하지만 EA 50 C5를 사용하면 출력은
012입니다.header must : 234 80 12 197
header read : 234 80 197 1
마지막 1은 다음 입력에 대한 적법 값입니다. 그렇다면 왜 다른가? 어떻게 수정해야합니까?
STL에는 'fstream'이 없었고, 내가 사용하는 것에 의심의 여지가 있습니다. – Fanael
@Fanael STL의 fstream을 작성 했습니까? 제목에서는 std :: fstream라고합니다. 그렇다면, 제목을 바꿀 수있는 사람들이 있습니까? – JoshuaBehrens
예, 그랬습니다. [개정 내역] (http://stackoverflow.com/posts/9469361/revisions)을 참조하십시오. – Fanael