English 中文(简体)
最新数据:在数据之后用流体书写的物体
原标题:Update ifstream object after data is written to its file using ofstream
  • 时间:2015-01-06 21:11:36
  •  标签:
  • c++
  • fstream

在测试我的代码时,我面临一个问题,即<条码>。 在其档案中记录更多的数据时,没有更新。 因此,这里是显示问题的样本法典:

    ifstream is(filename);
    string line;
    while (getline(is, line))
        cout << "line: " << line << endl;

    ofstream os(filename, ofstream::out | ofstream::app);
    string additional("additional");
    os << additional;
    os.flush();

    while (getline(is, line))
        cout << "line additional: " << line << endl;

No additional lines were written to stdout, though they are written to the file. I m not using fstream instead of a couple of if/ofstream because I need it like this for testing purposes.

如何使<代码>>>>>>>>>><>>> 查阅”文档中的改动?

www.un.org/Depts/DGACM/index_spanish.htm 我用<代码>clear方法对借方进行了清理。 该公司在我的乌兰巴托机器上安装了电灯。 但是,在我的MacOSX上,它的工作不多。 你们是否知道如何独立地建立平台?

最佳回答

You need to call std::ios::clear on the input stream after the first read. When you read the whole file, it sets the failbit in the stream and will refuse to keep reading, even if the file actually changed in the meantime.

ifstream is(filename);
string line;
while (getline(is, line))
    cout << "line: " << line << endl;

ofstream os(filename, ofstream::out | ofstream::app);
string additional("additional");
os << additional;
os.flush();

is.clear(); //< Now we can read again
while (getline(is, line))
    cout << "line additional: " << line << endl;
问题回答

当你在阅读<代码>is时,第一次在流体内错误状态中设置<>eofbit。

你们需要加以澄清,然后才能继续阅读<代码>is.clear()功能,该功能与内部错误状态相呼应。

我认为,原因是你用“上游:外派”开启了产出流文档。

ofstream os(filename, ofstream::out | ofstream::app);

如今,“上游:外派”一俟档案开放就开始登记。 这就是说,一旦开始,你档案的所有先前内容都将删除。 相反,请尝试:

ofstream os(filename, ios::app);

关于macOSclear(sync()。 申请文件的同时监督工作。

    std::ifstream ifs(filename);
    std::string line;

    while (run) {
        if (!ifs)
            std::cout << "ifs not open
";

        while (std::getline(ifs, line)) 
            std::cout << line << "
";
        ifs.clear();
        ifs.sync()
        
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }

xref:ward Hinnant Ifstream not see new content on OSX





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签