【问题标题】:Reading from file right after Writing in it写入文件后立即从文件中读取
【发布时间】:2016-11-09 20:08:15
【问题描述】:

我想写入文件,然后从中读取并打印结果

这是我的代码

#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char* argv[]){

int x,y;
ofstream fd1(argv[1]);
ifstream fd2(argv[1]);
cin>>x;
fd1<<x;
fd2>>y;
cout<<"just read "<<y<<endl;
fd1.close();
fd2.close();
return 0;
}

它有什么问题?我输入 123 它输出“just read -1078463800”

【问题讨论】:

  • 您应该检查您的文件操作是否成功完成(流的创建和读/写)。我的猜测是 ifstream 失败是因为 ofstream 保存了文件资源
  • 听起来像是 XY 问题。你到底想解决什么问题?
  • @Borgleader 我也这么认为,而且 Remy 坚持相反,我发现我对此非常错误(但不是关于缓冲,幸运的是!)。即使在 Windows 上,也可以这样做(它不像 python 或 C)。所以我今天学到了一些东西。

标签: c++ fstream ifstream ofstream


【解决方案1】:

即使您可以同时以读写方式打开,此写入操作也是缓冲,这意味着除非您刷新流(或关闭文件),否则它可能不会写入磁盘。

当然下面的代码可以完美运行:

#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char* argv[]){

int x,y;
ofstream fd1(argv[1]);

cin>>x;
fd1<<x;


fd1.close();

ifstream fd2(argv[1]);
if (fd2.good())
{
   cout << "read OK" << endl;
}
fd2>>y;
cout<<"just read "<<y<<endl;
fd2.close();

return 0;
}

【讨论】:

  • std::ofstreamstd::ifstream 可以同时打开同一个文件。我在 Windows 上试了一下,效果很好。真正的问题是在写入之后和读取之前缺乏缓冲区刷新。您不需要关闭std::ofstream,您可以直接调用fd1.flush()fd1 &lt;&lt; std::flush
  • @RemyLebeau 我的回答中已经包含了。你正在运行 Linux,所以你可以做到。在 Windows 上你不能。
  • 该死的你是对的!我做了错误的测试。编辑!!! (我在没有传递任何参数的情况下进行了测试,因此没有打开文件。被 OP 代码捕获)。
【解决方案2】:

fd2&gt;&gt;y 语句失败,因为尚未从文件中读取任何内容。 std::ofstream 缓冲其输出,并且在尝试从文件中读取之前,您尚未将缓冲区刷新到磁盘上的文件。

std::ofstream 在以下情况下刷新其缓冲区:

  1. 写入新行。

  2. 它的flush() 方法被直接调用或者当std::flush()std::endl 被流式传输到它时。

试试这个:

fd1 << x << flush;

或者:

fd1 << x;
fd1.flush();

顺便说一句,您确实应该在此过程中检查错误。 std::ofstreamstd::ifstream 构造函数可能无法创建/打开文件。 &lt;&lt;&gt;&gt; 运算符可能无法写入/读取值。所有这些操作都可以报告您可以检查的错误。例如:

#include <iostream>
#include <fstream>

int main(int argc, char* argv[])
{
    int x, y;
    std::ofstream fd1(argv[1]);
    std::ifstream fd2(argv[1]);

    if (!fd1.is_open())
    {
        std::cout << "cannot create output file" << std::endl;
    }
    else if (!fd2.is_open())
    {
        std::cout << "cannot open input file" << std::endl;
    }
    else if (!(std::cin >> x))
    {
        std::cout << "invalid input" << std::endl;
    }
    else if (!(fd1 << x << std::flush))
    {
        std::cout << "cannot write to output file" << std::endl;
    }
    else if (!(fd2 >> y))
    {
        std::cout << "cannot read from input file" << std::endl;
    }
    else
    {
        std::cout << "just read " << y << std::endl;
    }

    return 0;
}

或者:

#include <iostream>
#include <fstream>

int main(int argc, char* argv[])
{
    int x, y;

    std::ofstream fd1;
    std::ifstream fd2;

    fd1.exceptions(std::ofstream::failbit);
    fd2.exceptions(std::ifstream::failbit);
    std::cin.exceptions(std::ifstream::failbit);

    try
    {
        fd1.open(argv[1]);
        fd2.open(argv[1]);

        std::cin >> x;

        fd1 << x << std::flush;
        fd2 >> y;

        std::cout << "just read " << y << std::endl;
    }
    catch (const std::ios_base::failure &e)
    {
        std::cout << "error! " << e.what() << std::endl;
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    相关资源
    最近更新 更多