2010-03-29 34 views
7

Tôi đang cố gắng viết một chương trình có một tệp lớn (thuộc bất kỳ loại nào) và tách nó thành nhiều phần nhỏ hơn. Tôi nghĩ rằng tôi có ý tưởng cơ bản xuống, nhưng vì lý do nào đó tôi không thể tạo ra một kích thước chunk hơn 12 kb. Tôi biết có một vài giải pháp trên google, v.v. nhưng tôi quan tâm nhiều hơn đến việc tìm hiểu nguồn gốc của giới hạn này là gì sau đó thực sự sử dụng chương trình để chia nhỏ các tệp.Tách một Tệp Lớn Trong C++

//This file splits are larger into smaller files of a user inputted size. 
#include<iostream> 
#include<fstream> 
#include<string> 
#include<sstream> 
#include <direct.h> 
#include <stdlib.h> 
using namespace std; 

void GetCurrentPath(char* buffer) 
{ 
_getcwd(buffer, _MAX_PATH); 
} 


int main() 
{ 
// use the function to get the path 
char CurrentPath[_MAX_PATH]; 
GetCurrentPath(CurrentPath);//Get the current directory (used for displaying output) 

fstream bigFile; 
string filename; 
int partsize; 
cout << "Enter a file name: "; 
cin >> filename; //Recieve target file 
cout << "Enter the number of bites in each smaller file: "; 
cin >> partsize; //Recieve volume size 

bigFile.open(filename.c_str(),ios::in | ios::binary); 
bigFile.seekg(0, ios::end); // position get-ptr 0 bytes from end 
int size = bigFile.tellg(); // get-ptr position is now same as file size 
bigFile.seekg(0, ios::beg); // position get-ptr 0 bytes from beginning 

for (int i = 0; i <= (size/partsize); i++) 
{ 
    //Build File Name 
    string partname = filename; //The original filename 
    string charnum;  //archive number 
    stringstream out; //stringstream object out, used to build the archive name 
    out << "." << i; 
    charnum = out.str(); 
    partname.append(charnum); //put the part name together 

    //Write new file part 
    fstream filePart; 
    filePart.open(partname.c_str(),ios::out | ios::binary); //Open new file with the name built above 
    //Check if near the end of file 
    if (bigFile.tellg() < (size - (size%partsize))) 
    { 
    filePart.write(reinterpret_cast<char *>(&bigFile),partsize); //Write the selected amount to the file 
    filePart.close(); //close file 
    bigFile.seekg(partsize, ios::cur); //move pointer to next position to be written 
    } 
    //Changes the size of the last volume because it is the end of the file 
    else 
    { 
    filePart.write(reinterpret_cast<char *>(&bigFile),(size%partsize)); //Write the selected amount to the file 
    filePart.close(); //close file 
    } 
    cout << "File " << CurrentPath << partname << " produced" << endl; //display the progress of the split 
} 


bigFile.close(); 
cout << "Split Complete." << endl; 
return 0; 
} 

Bất kỳ ý tưởng nào?

Trả lời

4

Bạn đang ghi vào tệp phân tách nhưng không đọc từ tệp lớn. Những gì bạn đang viết nó trong cấu trúc trong bộ nhớ của bigfile, không phải là nội dung của bigfile. Bạn cần phân bổ một bộ đệm, đọc nó từ bigfile và viết nó vào (các) tệp phân tách.

+0

Đây chính xác là vấn đề, khiến những thay đổi này hoạt động hoàn hảo. Cảm ơn! – wdow88

Các vấn đề liên quan