如何在C ++中正确读取和覆盖相同的文件

时间:2015-09-18 15:40:32

标签: c++ file input stream

关于如何阅读,修改并将日期保存到同一个std::fstream处理文件的问题,如下所示:

std::fstream file(input_name.c_str(), std::ios::out | std::ios::in | std::ios::app | std::ifstream::binary);
std::vector<unsigned char> byte;
inputFile.seekg(0, file.end);
long long int length = file.tellg();
file.seekg(0, file.beg);
lldiv_t divresult = lldiv(length, blockInput);
for (long long int a = 0; a != divresult.quot; a++) {
    byte.reserve(blockInput / sizeof(unsigned char));
    byte.insert(byte.begin(), blockInput / sizeof(unsigned char), 0);
    file.read((char*)&byte[0], blockInput);
    for (int size_counter = 0; size_counter != blockInput; size_counter++) {
        byte[size_counter] = Transform(rounds, byte[size_counter], bytesUsed++);
    }
//the same as lower
    file.write((char*)&byte[0], blockInput);
    byte.clear();
}
if (divresult.rem > 0) {
    byte.reserve(divresult.rem / sizeof(unsigned char));
    byte.insert(byte.begin(), divresult.rem / sizeof(unsigned char), 0);
    file.read((char*)&byte[0], divresult.rem);
    for (int size_counter = 0; size_counter != divresult.rem; size_counter++) {
        byte[size_counter] = Transform(rounds, byte[size_counter], bytesUsed++);
    }
//what should be here?
//perhaps "file.seekg( file.tellg() - bufferSize );" but I'm not sure what to do next..
    file.write((char*)&byte[0], divresult.rem);
    byte.clear();
}
file.close();

你们是否知道如何正确地做到这一点?

0 个答案:

没有答案