2012-02-10 4 views
1

파일을 압축하지 않고 여러 파일로 분할해야합니다. 내가 찾은 CPP 참조C++에서 파일 분할

#include <fstream> 
using namespace std; 

int main() { 

char * buffer; 
long size; 

ifstream infile ("test.txt",ifstream::binary); 
ofstream outfile ("new.txt",ofstream::binary); 

// get size of file 
infile.seekg(0,ifstream::end); 
size=infile.tellg(); 
infile.seekg(0); 

// allocate memory for file content 
buffer = new char [size]; 

// read content of infile 
infile.read (buffer,size); 

// write to outfile 
outfile.write (buffer,size); 

// release dynamically-allocated memory 
delete[] buffer; 

outfile.close(); 
infile.close(); 
return 0; 
} 

나는 이것을 이렇게 생각했습니다. 하지만 문제는 .. 파일의 시작 부분에서만 데이터를 읽을 수 있기 때문에 첫 번째 파일 만 만들 수 있습니다. 이 파일을 분할하는 가장 좋은 방법은 무엇입니까?

+0

왜 파일의 시작 부분에서만 데이터를 읽을 수 있습니까? –

+0

글쎄, 나는 파일의 다른 곳에서 그것을 읽는 법을 모른다. – Transcendental

답변

0

원하는 위치로 스트림을 검색 한 다음 스트림을 읽을 수 있습니다. 이 코드를 확인하십시오.

// get size of file 
infile.seekg(0,ifstream::end); 
size=infile.tellg(); 
infile.seekg(0); 

당신이해야 할 모든 당신이 INFILE, 가까운 OUTFILE를 열고 새 OUTFILE을 읽는 중지 된 위치, 위치를 기억 버퍼를 재 할당 및 버퍼 및 초 OUTFILE에 쓸 INFILE 읽는 것입니다. 바퀴를 재발견 왜

0

파일의 아무 곳에서나 데이터를 읽을 수 있습니다. 이미 끝까지 이동 했으므로 성공적으로 시작합니다.

당신은 그래도 필요하지 않습니다. 각각의 outputSize을 순차적으로 읽고 약간의 outputSize < size에 대해 새 파일에 쓰는 루프를 작성하십시오.

0

- 당신은 내가 당신의 문제에 대한 해결책을 가지고 생각 C++

0

에 구현하려는 경우 당신이 아이디어를 얻을 수 있도록 split

시도는 심지어 소스 코드가 ... char 배열의 모든 첫 번째 파일을 읽습니다. 이 그럼 당신은 예를 들어

... 파일에 배열의 첫 번째 절반을 작성하고, 다른 파일에 당신 배열의 다음 하반기 : 또한 상반기를 읽을 수

#include <fstream> 
using namespace std; 

int main() { 

char * buffer; 
long size; 

ifstream infile ("test.txt",ifstream::binary); 
ofstream outfile ("new.txt",ofstream::binary); 
ofstream outfile2 ("new2.txt",ofstream::binary); 

// get size of file 
infile.seekg(0,ifstream::end); 
size=infile.tellg(); 
infile.seekg(0); 

// allocate memory for file content 
buffer = new char [size]; 

// read content of infile 
infile.read (buffer,size); 

// write to outfile 
outfile.write (buffer,size/2); 
outfile2.write (buffer+size/2,size); 

// release dynamically-allocated memory 
delete[] buffer; 

outfile.close(); 
infile.close(); 
outfile2.close(); 
return 0; 
} 

, 쓰기 그런 다음 두 번째 반쪽을 읽고 그것을 쓰십시오. 그냥보십시오.

int main() { 

char * buffer; 
long size; 
long halfSize; 
ifstream infile ("test.txt",ifstream::binary); 
ofstream outfile ("new.txt",ofstream::binary); 
ofstream outfile2 ("new2.txt",ofstream::binary); 

// get size of file 
infile.seekg(0,ifstream::end); 
size=infile.tellg(); 
infile.seekg(0); 
halfSize = static_cast<int>(floor(size/2)); 
// allocate memory for file content 

buffer1 = new char[halfSize]; 
buffer2 = new char[size-halfSize]; 

// read content of infile 
infile.read (buffer1,halfSize); 
infile.seekg(halfSize+1); 
// read content of infile 
infile.read (buffer2,size-halfSize); 

// write to outfile 
outfile.write (buffer1,halfSize); 
outfile2.write (buffer2,size-halfSize); 

// release dynamically-allocated memory 
delete[] buffer; 
delete[] buffer2; 

outfile.close(); 
infile.close(); 
outfile2.close(); 
return 0; 
} 
1

예제 코드는 파일을 여러 파일로 분할하지 않습니다. 그냥 파일을 복사합니다. 파일을 여러 파일로 분할하려면 입력을 닫지 마십시오. 의사 코드에서 각 앞의 읽기가 종료 정확히 종목을 읽을 수 있도록

open input 
decide size of each block 
read first block 
while block is not empty (read succeeded): 
    open new output file 
    write block 
    close output file 
    read another block 

중요한 부분은, 입력 파일을 닫을 수 없습니다.