【问题标题】:How to properly read and overwrite the same file in C++如何在 C++ 中正确读取和覆盖同一个文件
【发布时间】:2015-12-15 20:07:34
【问题描述】:

关于如何读取、即时修改和保存日期到同一个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();

你们知道正确的做法吗?

【问题讨论】:

  • file.seekg(0, std::ios::beg);
  • 现在file.tellg() - bufferSize 有意义。
  • @Alvarey std::ios::beg 是不可接受的,因为读取的是指定大小的块而不是整个文件。

标签: c++ file input stream


【解决方案1】:

不,答案是通过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();

看起来很简单,但是很难找出问题的实质。我希望有人能够在某处使用它:)

【讨论】:

    猜你喜欢
    • 2015-09-20
    • 1970-01-01
    • 2021-12-07
    • 2011-02-14
    • 2013-11-21
    • 2019-10-23
    • 2013-11-27
    相关资源
    最近更新 更多