以下是我将文件从源文件复制到目的地的功能
void CopyFile(const std::string& source, const std::string& destination)
{
std::ifstream ifs(source.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if (!ifs.good())
return;
std::ofstream ofs(destination.c_str(), std::ios::out | std::ios::binary);
const int bufferSize = 4;
char buffer[bufferSize];
std::streamoff streamSize = ifs.tellg();
std::streamoff iteration = streamSize / bufferSize;
int rem = streamSize % bufferSize;
ifs.seekg(std::ios::beg);
while (iteration > 0)
{
ifs.read(buffer, bufferSize);
ofs.write(buffer, bufferSize);
ifs.seekg(bufferSize, std::ios::cur);
ofs.seekp(bufferSize, std::ios::cur);
--iteration;
}
ifs.read(buffer, rem);
ofs.write(buffer, rem);
ifs.close();
ofs.close();
}
其他方法,我以非二进制模式打开文件为我工作但不是这个。我在这里失踪了什么?