【问题标题】:edit: trouble checking if file is empty or not, what am I doing wrong?编辑:检查文件是否为空时遇到问题,我做错了什么?
【发布时间】:2016-03-06 03:57:05
【问题描述】:

编辑:将我的问题更改为更准确的情况

我正在尝试打开一个文本文件(如果它不存在则创建它,如果它不存在则打开它)。它是与输出相同的输入文件。

ofstream oFile("goalsFile.txt");
fstream iFile("goalsFile.txt");
string goalsText;   
string tempBuffer;
//int fileLength = 0;
bool empty = false;

if (oFile.is_open())
{
    if (iFile.is_open())
    {
        iFile >> tempBuffer;
        iFile.seekg(0, iFile.end);
        size_t fileLength = iFile.tellg();
        iFile.seekg(0, iFile.beg);
        if (fileLength == 0) 
        {
            cout << "Set a new goal\n" << "Goal Name:"; //if I end debugging her the file ends up being empty
            getline(cin, goalSet);
            oFile << goalSet;
            oFile << ";";
            cout << endl;

            cout << "Goal Cost:";
            getline(cin, tempBuffer);
            goalCost = stoi(tempBuffer);
            oFile << goalCost;
            cout << endl;
        }
    }
}

几个问题。一方面,如果文件存在并且其中有文本,它仍然会进入通常会要求我设定新目标的 if 循环。我似乎无法弄清楚这里发生了什么。

【问题讨论】:

  • 叹息.. 为什么投反对票?
  • 追到底,拿到位置。如果为零,则文件为空。
  • 是的,但是为什么我的工作没有工作,为什么发生了什么,发生了什么?。
  • @JoachimPileborg 我尝试使用我所理解的 seek 到最后,但它仍然不起作用,有什么提示吗?

标签: c++ fileinputstream


【解决方案1】:

问题只是您使用的是缓冲的 IO 流。尽管它们在下面引用相同的文件,但它们具有完全独立的缓冲区。

// open the file for writing and erase existing contents.
std::ostream out(filename);
// open the now empty file for reading.
std::istream in(filename);
// write to out's buffer
out << "hello";

此时,“hello”可能还没有写入磁盘,唯一的保证是它在out的输出缓冲区中。要强制将其写入磁盘,您可以使用

out << std::endl;  // new line + flush
out << std::flush; // just a flush

这意味着我们已将输出提交到磁盘,但此时输入缓冲区仍未触及,因此文件似乎仍为空。

为了让您的输入文件看到您写入输出文件的内容,您需要使用sync

#include <iostream>
#include <fstream>
#include <string>

static const char* filename = "testfile.txt";

int main()
{
    std::string hello;

    {
        std::ofstream out(filename);
        std::ifstream in(filename);
        out << "hello\n";
        in >> hello;
        std::cout << "unsync'd read got '" << hello << "'\n";
    }

    {
        std::ofstream out(filename);
        std::ifstream in(filename);
        out << "hello\n";

        out << std::flush;
        in.sync();

        in >> hello;
        std::cout << "sync'd read got '" << hello << "'\n";
    }
}

尝试使用缓冲流执行此操作时遇到的下一个问题是,每次将更多数据写入文件时,都需要clear() 输入流上的 eof 位...

【讨论】:

  • 关于这个和我的代码,然后不尝试读取内容并检查它是否为空,它总是为空的?考虑到我想检查它是否已经有内容然后决定如何处理它(在这种情况下从用户那里获取目标)。通过使用同步,然后进行检查,您认为这可能是我的解决方案吗?我不确定如何根据我的代码情况使用“同步”。
  • 您发布的代码首先以非附加模式打开文件以进行写入,这实际上使文件为空。如果您想知道文件是否为空,请在打开它进行写入之前对其进行测试。 1/ 测试是否可以打开,即文件是否存在; 2/ 测试你是否在eof而不读,如果是,那么文件是空的。
  • OMG 追加模式。这是所有问题的全部问题,谢谢你,我是如何忽视这一点的。不过,感谢您的同步想法,这是我从未研究过的一件非常巧妙的事情
【解决方案2】:

试试Boost::FileSystem::is_empty,它会测试你的文件是否为空。我在某处读到使用 fstream 不是测试空文件的好方法。

【讨论】:

  • 我不确定那到底是什么:/
  • @Robolisk 是一个可移植的库,具有很好的模板和功能来执行平台无关的任务,例如文件存在、空文件、文件大小等,网上有很多关于如何使用它们的例子,比如one,它展示了一些带有文件的库示例。
猜你喜欢
  • 1970-01-01
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多