2013-11-14 2 views
0

바이트의 청크에서 파일을 작성,하지만 난 IOS : CUR을 사용하는 방법을 이해 할 수없는 것. 나는 전체 파일을 10 바이트 덩어리로 읽고, 그 바이트를 버퍼에 쓴 다음 그 버퍼를 다른 파일에 쓴다. 그것을 위해 나는 처음 10 바이트를 보내고 다음 10 바이트를 보낸다. 하지만 포인터가 마지막 반복 위치에서 시작하는지 어떻게 확인할 수 있습니까?읽고 C++ 내가 어디에서나 검색 한

char* data = 0; 
int i = 0; 
std::ifstream is("test.txt", std::ifstream::binary); 

if (is) 
{ 
    is.seekg(0, is.end); 
    int size = is.tellg(); 

    cout << size << endl; 

    ofstream obj; 
    obj.open("new.txt"); 

    while (!is.eof()) 
    { 
     for (; i <= size;) 
     { 
      is.seekg(i, is.beg); 
      int sz = is.tellg(); 
      // cout<<sz<<endl; 

      data = new char[sz + 1]; // for the '\0' 
      is.read(data, sz); 

      data[sz] = '\0'; // set '\0' 
      cout << " data size: " << strlen(data) << "\n"; 
      cout << data; 
      i = i + 10; 
     } 
     obj.close(); 
    } 
} 

답변

2

파일 위치의 위치를 ​​변경할 필요가 없습니다.

모든 읽기 작업 후에 파일 위치가 업데이트됩니다.

두 개의 파일 객체를 사용해야합니다. 하나는 출력용으로 다른 객체를 입력해야합니다. 두 경우 모두

는 파일 위치는 각각의 읽기 및 쓰기 작업 후 업데이트됩니다.

편집 1

: 간단한 예

#define BUFFER_SIZE 16 
unsigned char buffer[BUFFER_SIZE]; 
//... 
while (input_file.read((char *)buffer, BUFFER_SIZE)) 
{ 
    output_file.write((char *)buffer, BUFFER_SIZE); 
} 

INPUT_FILE 0 오프셋 후 제 1 판독 후에, 파일 위치 (16)이있을 것에서의 위치 인 경우에는 확인할 수있다

int read_file_position = input_file.tellg(); 
cout << "input file position: " << read_file_position << endl; 
while (input_file.read((char *)buffer, BUFFER_SIZE)) 
{ 
    read_file_position = input_file.tellg(); 
    cout << "input file position: " << read_file_position << endl; 
    output_file.write((char *)buffer, BUFFER_SIZE); 
} 
+0

나는,이 개체가가 " "읽기, obj 쓰기. "파일 위치를 변경할 필요가 없습니다." 모든 읽기 작업 후에 파일 위치가 업데이트됩니다. " 붙여 넣은 코드에서 처음 10 바이트는 인쇄되지 않습니다. 나는 그 이유를 이해하지 못한다. :( – Basilisk

+0

내 대답에 내 추가를 참조하십시오 –

1

나는 그것이 그에게 간단하게 할 수 있다고 생각 :

char* data = 0; 
int i = 0; 
int size = 0; 
int sz = 10; 
std::ifstream is("test.txt", std::ifstream::binary); 
std::ofstream obj; 
data = new char[sz+1]; 
int read_sz = 0; 

if (is) 
{ 
    obj.open("new.txt"); 
    read_sz = is.tellg(); //we are at the beginning of the file now 
    while (!is.eof()) 
    { 
     int old_read_sz = read_sz; // old position - before next read 
     is.read(data, sz); 
     read_sz = is.tellg(); // new position, we've read read_sz-old_read_sz bytes 
     data[read_sz-old_read_sz] = '\0'; // set '\0' 
     // I hope you wanted the two lines below only for debugging purposes :) 
     cout << " data size so far: " << read_sz << "\n"; 
     cout << data << endl; 
     obj.write(data, read_sz-old_read_sz); 
    } 
    obj.close(); 
    is.close(); 
    cout << "total data size: "<< read_sz << endl; 
} 

데이터 청크를 읽은 후에는 파일 커서가 이미 해당 청크를지나 갔기 때문에 직접 위치를 재조정 할 필요가 없습니다 (현재 위치에서 시작/끝으로 이동하면 비용이 많이 드는 경우가 있습니다 - 특히 당신은 큰 입력 파일을 가지고있다). http://www.cplusplus.com/doc/tutorial/files/

업데이트 : 그런데

, 여기에 주제에 대한 튜토리얼입니다 잊었던 is.read() = 오래된 C 스타일 읽기 : -}

+0

데이터 = 새로운 숯불 [SZ + 1]. // 제로 오 참 –

+0

(더 좋을 것이다) 실제로 표준 : : 문자열 (데이터, read_sz) 디버깅 출력을 생성하기 위해, 그 의도 - 고정 : – Ashalynd

+0

와우. 예, 디버깅 용이었습니다. 그러나, 나는이 오류를 받고 있어요 "에서 변환 할 수 없습니다 '클래스 표준 : : basic_istream보다는 <문자, 구조체 표준 : : char_traits >' 'INT'에"라인에서 "INT read_sz = is.read (데이터, SZ) ; " read_sz이 (가) 무엇을하고 있는지 말해 주실 수 있나요? – Basilisk

관련 문제