fstream ifstream ofstream分块读写文件
#include <fstream> #include <algorithm> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { const int BUFFER_SIZE = 1024 * 10; std::ifstream ifs("e://setup_10.2.0.2001s.exe", ios::binary); std::ofstream ofs("e://setup_10.2.0.2001s2.exe", ios::binary); if (ifs) { ifs.seekg(0, ifs.end); int length = (int)ifs.tellg(); ifs.seekg(0, ifs.beg); std::string buffer(BUFFER_SIZE, '\0'); int offset = 0; int readSize = min(BUFFER_SIZE, length - offset); while (readSize > 0 && ifs.read(&buffer[0], readSize)) { ofs.write(&buffer[0], readSize); offset += readSize; readSize = min(BUFFER_SIZE, length - offset); std::cout << "offset:" << offset << std::endl; } } ifs.close(); ofs.close(); system("pause"); return 0; }
参考链接:fstream ifstream ofstream分块读写文件