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



如何读取,修改和保存日期到相同的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();

你们知道怎么做正确吗?

不,答案是修改std::istream::seekgstd::istream::seekp在我标记的特定位置的读和写流位置:

std::fstream file(input.c_str(), std::ios::in | std::ios::out | std::ifstream::binary);
        std::vector<unsigned char> byte;
        file.seekg(0, file.end);
        long long int length = file.tellg();
        file.seekg(0, file.beg);
        file.seekp(0, file.beg);
        lldiv_t divresult = lldiv(length, blockInput);
        for (long long int a = 0; a != divresult.quot; a++) {
            std::vector<unsigned char> byte;
            byte.reserve(blockInput / sizeof(unsigned char));
            byte.insert(byte.begin(), blockInput / sizeof(unsigned char), 0);
//HERE
            file.seekp(a*blockInput, std::ios_base::beg);
            file.read((char*)&byte[0], blockInput);
            for (int size_counter = 0; size_counter != blockInput; size_counter++) {
                byte[size_counter] = Transform(rounds, byte[size_counter]);
            }
//HERE
            file.seekg(a*blockInput, std::ios_base::beg);
            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);
//HERE
            file.seekp(divresult.quot*blockInput, std::ios_base::beg);
            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]);
            }
//AAAND HERE
            file.seekg(divresult.quot*blockInput, std::ios_base::beg);
            file.write((char*)&byte[0], divresult.rem);
            byte.clear();
        }
        file.close();

似乎很容易,但很难找出问题到底是什么。我希望有人能在某个地方使用它。

相关内容

  • 没有找到相关文章

最新更新