简体   繁体   English

读取和写入同一文件C ++

[英]reading and writing to the same file c++

I have small app which at begin will read text file (with serialized objects) in which im storing some obejcts (im doing it by overloading << and >> operators). 我有一个小应用程序,一开始它会读取文本文件(带有序列化的对象),在该文件中我存储了一些对象(即通过重载<<和>>运算符来实现)。 This text file has to be updated each time new object is created: 每次创建新对象时,都必须更新此文本文件:

fstream m_haystackMapFile;
m_haystackMapfile.open(haystackMapFile,  std::ios::binary | std::ios::in | std::ios::out);

first i do read: 首先,我确实阅读:

WHaystackFile f;
m_haystackMapfile.seekg(0, std::ios::beg);

std::copy(std::istream_iterator<WHaystackFile>(m_haystackMapfile), std::istream_iterator<WHaystackFile>(), std::back_inserter(m_haystackFiles));

std::cout << "reading input file, no of files: " << m_haystackFiles.size() << std::endl;
for(std::vector<WHaystackFile>::iterator it = m_haystackFiles.begin(); it != m_haystackFiles.end(); ++it){
    f = *it;
    std::cout << "key: " << f.getKey() << " cookie: " << f.getCookie()  << " path: " << f.getPath()  << " size: " << f.getSize()<< std::endl;
}

then after creating new object I do write: 然后在创建新对象后,我写:

void WhaystackMap::addEntry(WHaystackFile &f){
    std::cout << "adding entry to index file" << std::endl;
    m_haystackMapfile.seekp(std::ios::end);
    m_haystackMapfile << f;
    std::cout << f;

}

unfortunatelly file in which I want to write is never updated and it always has size 0. Maybe im messing up something, but after googling i can't find answer how to using fstream I can read and write to same file... 不幸的是,我要写入的文件永远不会更新,并且大小始终为0。也许我在弄乱某些东西,但是在谷歌搜索之后,我找不到如何使用fstream的答案,我可以读写同一文件...

any help is welcomed :) 任何帮助都欢迎:)

regards J. 问候J.

It's very important to check success of I/O operations. 检查I / O操作是否成功非常重要。 For example, if seekp is unable to seek to the desired position, it will set the failbit , and then all subsequent writes will fail. 例如,如果seekp无法搜索到所需位置,它将设置failbit ,然后所有后续写入都会失败。 Or, as @Christophe points out, if you read the file to the end, you will cause the eofbit to be set. 或者,就像@Christophe指出的那样,如果您将文件读到最后,则会导致设置eofbit。 Unless that bit is cleared, the next I/O operation (even seekp) will fail. 除非将该位清除,否则下一个I / O操作(甚至seekp)将失败。

Even if the eofbit had been reset, the seek probably would fail because the call should have been m_haystackMapfile.seekp(0, std::ios::end); 即使重新设置了eofbit,搜索也可能会失败,因为调用应该是m_haystackMapfile.seekp(0, std::ios::end); .

The problem is the wrong usage of seekp() : 问题是seekp()用法错误:

  • you use it with one single parameter ios::end in m_haystackMapfile.seekp(std::ios::end) 您将其与m_haystackMapfile.seekp(std::ios::end)单个参数ios::end一起使用
  • but single parameter only works for absolute positionning. 但单个参数仅适用于绝对定位。 So ios::end is converted into an integer and will locate you at an unexpected place (on my implementation it's 2). 因此ios::end被转换为整数,并将您放置在意外的位置(在我的实现中为2)。
  • you have to use m_haystackMapfile.seekp(0, std::ios::end) instead 您必须使用m_haystackMapfile.seekp(0, std::ios::end)代替

There is another problem: the istream_iterator<>() that you use in your std::copy() will read the stream until it reaches its end. 还有另一个问题:您在std::copy()使用的istream_iterator<>() std::copy()将读取流,直到流结束。 So the failbit and eofbit will be set. 因此,将设置故障位和eofbit。

Connsequently, no stream operation will succed until you clear the flags: m_haystackMapfile.clear(); 因此,除非清除标志,否则流操作不会成功: m_haystackMapfile.clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM