2011-10-01 10 views
-1

파일에 쓰기 전에 이진 데이터를 임시로 캐시하고 싶습니다. 이것은 내 생각이었다.파일에 이진 데이터 쓰기

헤더 다음에 오는 데이터의 양을 나타내는이 데이터 앞에 헤더를 삽입해야하므로이 데이터를 ofstream file에 기록하기 전에이 데이터를 캐시에 보관하는 방법이 필요했습니다. 나는 ostream buffer();을 작성하여 파일에 쓰지 않고이 모든 데이터를 덤프 할 수 있습니다.

헤더를 작성한 후 데이터를 덤프하려면 file << buffer을 수행하면됩니다.

난 아직도 컴파일러 오류와 사투를 벌인거야

같은 일이 같이

error: no matching function for call to ‘TGA2Converter::writePixel(std::ostream (&)(), uint32_t&)’ 
note: candidate is: void TGA2Converter::writePixel(std::ostream&, uint32_t) 

왜이 메시지는 무엇입니까? 그리고 아마도 더 중요한 것은, 제가 가장 효과적이고 편리한 방법으로 문제에 접근하고 있습니까?


편집 :명 코드를 요구하고있다. 나는

// This is a file. I do not want to write the binary 
// data to the file before I can write the header. 
ofstream file("test.txt", ios::binary); 

// This is binary data. Each entry represents a byte. 
// I want to write it to a temporary cache. In my 
// real code, this data has to be gathered before 
// I can write the header because its contents depend 
// on the nature of the data. 
stringstream cache; 
vector<uint32_t> arbitraryBinData; 
arbitraryBinData.resize(3); 
arbitraryBinData[0] = 0x00; 
arbitraryBinData[1] = 0xef; 
arbitraryBinData[2] = 0x08; 

// Write it to temporary cache 
for (unsigned i = 0; i < arbitraryBinData.size(); ++i) 
    cache << arbitraryBinData[i]; 

// Write header 
uint32_t header = 0x80;  // Calculation based on the data! 
file << header; 

// Write data from cache 
file << cache; 

나는 완전히 파일에 기록이 이진 데이터를 예상 ...이로 범위를 좁힐려고 :

0000000: 8000 ef08 

하지만이있어 :

0000000: 3132 3830 7837 6666 6638 6434 3764 3139 
0000010: 38 

을 예상되는 결과를 얻지 못하는 이유는 무엇입니까?

+2

몇 가지 코드를 게시하십시오. –

+0

질문을 업데이트했습니다. 바라기를 바꿔서 내가하려는 일을 해소 해 주겠다. – Pieter

답변

3

ostream buffer();buffer이라는 함수를 선언하고 인수를 사용하지 않고 ostream을 반환합니다. 또한 ostream은 기본 클래스이므로 strstream을 대신 사용해야합니다.

+0

아참. 나는 생성자가 보호 되었기 때문에'ostream buffer; '가 작동하지 않는다는 것을 알아 채고 괄호를 추가했다. 나는 명확하게 생각하고 있지 않았다. 나는'strstream'을 시도 할 것이지만, 파일의 바이너리 인코딩을 망가 뜨리지 않기를 바랍니다. – Pieter

관련 문제