C++ 如何正确读取和覆盖C++;

C++ 如何正确读取和覆盖C++;,c++,file,input,stream,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.tel

如何读取、动态修改并将日期保存到相同的
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::fstream文件(输入_name.c_str(),std::ios::out | std::ios::in | std::ios::app | std::ifstream::binary);
std::向量字节;
inputFile.seekg(0,file.end);
long int length=file.tellg();
file.seekg(0,file.beg);
lldiv\u t divresult=lldiv(长度,块输入);
for(long-long int a=0;a!=divresult.quot;a++){
字节保留(块输入/sizeof(无符号字符));
byte.insert(byte.begin(),blockInput/sizeof(unsigned char),0);
读取((字符*)&字节[0],块输入);
对于(int size\u counter=0;size\u counter!=blockInput;size\u counter++){
字节[大小计数器]=转换(四舍五入,字节[大小计数器],字节使用++);
}
//与较低的相同
write((char*)和字节[0],blockInput);
byte.clear();
}
如果(divresult.rem>0){
byte.reserve(divresult.rem/sizeof(unsigned char));
byte.insert(byte.begin(),divresult.rem/sizeof(unsigned char),0);
read((char*)和byte[0],divresult.rem);
for(int size\u counter=0;size\u counter!=divresult.rem;size\u counter++){
字节[大小计数器]=转换(四舍五入,字节[大小计数器],字节使用++);
}
//这里应该有什么?
//也许是“file.seekg(file.tellg()-bufferSize);”但我不确定下一步该怎么办。。
file.write((char*)和字节[0],divresult.rem);
byte.clear();
}
file.close();

你们知道如何正确操作吗?

不,答案是在我标记的特定位置,通过
std::istream::seekg
std::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();
std::fstream文件(input.c_str(),std::ios::in | std::ios::out | std::ifstream::binary);
std::向量字节;
seekg(0,file.end);
long int length=file.tellg();
file.seekg(0,file.beg);
seekp(0,file.beg);
lldiv\u t divresult=lldiv(长度,块输入);
for(long-long int a=0;a!=divresult.quot;a++){
std::向量字节;
字节保留(块输入/sizeof(无符号字符));
byte.insert(byte.begin(),blockInput/sizeof(unsigned char),0);
//这里
seekp(a*blockInput,std::ios\u base::beg);
读取((字符*)&字节[0],块输入);
对于(int size\u counter=0;size\u counter!=blockInput;size\u counter++){
字节[大小计数器]=转换(四舍五入,字节[大小计数器]);
}
//这里
seekg(a*blockInput,std::ios\u base::beg);
write((char*)和字节[0],blockInput);
byte.clear();
}
如果(divresult.rem>0){
byte.reserve(divresult.rem/sizeof(unsigned char));
byte.insert(byte.begin(),divresult.rem/sizeof(unsigned char),0);
//这里
seekp(divresult.quot*blockInput,std::ios_base::beg);
read((char*)和byte[0],divresult.rem);
for(int size\u counter=0;size\u counter!=divresult.rem;size\u counter++){
字节[大小计数器]=转换(四舍五入,字节[大小计数器]);
}
//啊,这里呢
seekg(divresult.quot*blockInput,std::ios_base::beg);
file.write((char*)和字节[0],divresult.rem);
byte.clear();
}
file.close();

看起来很容易,但很难找出问题的实质。我希望有人能在某个地方使用它:)

不,答案是通过
std::istream::seekg
std::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();
std::fstream文件(input.c_str(),std::ios::in | std::ios::out | std::ifstream::binary);
std::向量字节;
seekg(0,file.end);
long int length=file.tellg();
file.seekg(0,file.beg);
seekp(0,file.beg);
lldiv\u t divresult=lldiv(长度,块输入);
for(long-long int a=0;a!=divresult.quot;a++){
std::向量字节;
字节保留(块输入/sizeof(无符号字符));
byte.insert(byte.begin(),blockInput/sizeof(unsigned char),0);
//这里
seekp(a*blockInput,std::ios\u base::beg);
读取((字符*)&字节[0],块输入);
对于(int size\u counter=0;size\u counter!=blockInput;size\u counter++){
字节[大小计数器]=转换(四舍五入,字节[大小计数器]);
}
//这里
seekg(a*blockInput,std::ios\u base::beg);
write((char*)和字节[0],blockInput);
byte.clear();
}
如果(divresult.rem>0){
byte.reserve(divresult.rem/sizeof(unsigned char));
byte.insert(byte.begin(),divresult.rem/sizeof(unsigned char),0);
//这里
seekp(divresult.quot*blockInput,std::ios_base::beg);
read((char*)和byte[0],divresult.rem);
for(int size\u counter=0;size\u counter!=divresult.rem;size\u counter++){
字节[大小计数器]=转换(四舍五入,字节[大小计数器]);
}
//啊,这里呢
seekg(divresult.quot*blockInput,std::ios_base::beg);
file.write((char*)和字节[0],divresult.rem);
byte.clear();
}
file.close();

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

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