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

C++ 同时写入和读取文件,c++,ifstream,ofstream,C++,Ifstream,Ofstream,我有两个过程。一个人写入一个文件,他必须从中读取(同时…)。因此,在给定的时间,文件有两个fstreams打开(尽管它们可能处于不同的进程中)。 我编写了一个简单的测试函数来粗略地实现我需要的功能: void test_file_access() { try { std::string file_name = "/Users/xxxx/temp_test_folder/test_file.dat"; std::ofstream out(file_nam

我有两个过程。一个人写入一个文件,他必须从中读取(同时…)。因此,在给定的时间,文件有两个
fstream
s打开(尽管它们可能处于不同的进程中)。 我编写了一个简单的测试函数来粗略地实现我需要的功能:

void test_file_access()
{
    try {
        std::string file_name = "/Users/xxxx/temp_test_folder/test_file.dat"; 

        std::ofstream out(file_name,
                          std::ios_base::out | std::ios_base::app | std::ios_base::binary);
        out.write("Hello\n", 7);

        std::this_thread::sleep_for(std::chrono::seconds(1));

        std::array<char, 4096> read_buf;
        std::ifstream in(file_name,
                         std::ios_base::in | std::ios_base::binary);

        if (in.fail()) {
            std::cout << "Error reading file" << std::endl;
            return;
        }

        in.exceptions(std::ifstream::failbit | std::ifstream::badbit);

        //Exception at the below line.
        in.read(read_buf.data(), read_buf.size());
        auto last_read_size = in.gcount();
        auto offset = in.tellg();

        std::cout << "Read [" << read_buf.data() << "] from file. read_size = " << last_read_size
                  << ", offset = " << offset << std::endl;

        out.write("World\n", 7);

        std::this_thread::sleep_for(std::chrono::seconds(1));

        //Do this so I can continue from the position I was before?
        //in.clear();

        in.read(read_buf.data(), read_buf.size());
        last_read_size = in.gcount();
        offset = in.tellg();

        std::cout << "Read [" << read_buf.data() << "] from file. read_size = " << last_read_size
                  << ", offset = " << offset << std::endl;

        //Remove if you don't have boost.
        boost::filesystem::remove(file_name);
    }
    catch(std::ios_base::failure const & ex)
    {
        std::cout << "Error : " << ex.what() << std::endl;
        std::cout << "System error : " << strerror(errno) << std::endl;
    }
}


int main()
{
    test_file_access();
}

所以有两个问题,

  • 这里出了什么问题?为什么我得到一个
    操作超时
    错误
  • 这是一个不正确的尝试去做我需要做的事情吗?如果是,这里有什么问题

  • 您将7个字节写入该文件,然后尝试读取4096个字节。所以in-stream将只读取7个字节,并根据请求引发异常。请注意,如果捕获到此异常,其余代码将正确执行,例如,
    last\u read\u size
    将为7,并且您可以访问缓冲区中的这7个字节。

    构建两个这样的fstream对象以同时处理读写操作,往往会弄乱文件结束标志。要这样做,你必须在读之前使用
    seekg()
    ,在写之前使用
    seekp()
    。@Jon:这是否意味着对于
    ifstream
    ,我必须
    打开,阅读,记住位置,关闭--打开,寻找位置,阅读,记住位置,关闭
    ?对于流的
    打开、追加、关闭
    ?不,您不必关闭文件。例如,
    seekg()
    允许您执行诸如
    inputStream.seekg(0,inputStream.end)之类的操作
    这意味着如果在
    read()
    之前调用它,它会考虑上一个
    write()
    @Jon:尝试了你的建议,仍然是相同的错误。请注意,首先,我仅在第一次写入后才打开
    in
    流。不,如果我注释掉
    in.exceptions(…
    行),这就是在后续
    cout
    中打印的内容:
    读取[V]�] 从file.read_size=0,offset=-1
    好的,您需要刷新
    out
    缓冲区以确保更改可见。我只是想指出正确引发了异常。好的,但是
    out.flush()
    写入后立即生效也没有效果。相同的结果。我无法重现此结果。在每次写入后添加
    刷新
    并将读取字节数设置为7(而不是4096)后,此代码工作正常。好的,我犯了一个错误,在那里有一个
    seekg
    。删除它后,第一次读取正常。我将检查其余部分。
    Error : ios_base::clear: unspecified iostream_category error
    System error : Operation timed out