C++ 编辑:检查文件是否为空时出现问题,我做错了什么?

C++ 编辑:检查文件是否为空时出现问题,我做错了什么?,c++,fileinputstream,C++,Fileinputstream,编辑:将我的问题更改为更准确的情况 我试图打开一个文本文件(如果它不存在就创建它,如果它不存在就打开它)。它是与输出相同的输入文件 ofstream oFile("goalsFile.txt"); fstream iFile("goalsFile.txt"); string goalsText; string tempBuffer; //int fileLength = 0; bool empty = false; if (oFile.is_open()) { if (iFile

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

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

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;
        }
    }
}
文件流的
(“goalsFile.txt”);
fstream iFile(“goalsFile.txt”);
字符串目标文本;
字符串缓冲区;
//int fileLength=0;
bool empty=false;
如果(文件的)是打开的()
{
如果(iFile.is_open())
{
iFile>>临时缓冲区;
iFile.seekg(0,iFile.end);
size_t fileLength=iFile.tellg();
iFile.seekg(0,iFile.beg);
if(fileLength==0)
{

如果您的文件是空的,请尝试哪种测试。我在某个地方读到,使用fstream不是测试空文件的好方法。

问题只是您使用的是缓冲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";
这意味着我们已经将输出提交到磁盘,但此时输入缓冲区仍然未被触及,因此文件仍然看起来是空的

为了让您的输入文件看到您写入输出文件的内容,您需要使用
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";
    }
}
#包括
#包括
#包括
静态常量char*filename=“testfile.txt”;
int main()
{
字符串hello;
{
std::流输出(文件名);
std::ifstream-in(文件名);
出>你好;

std::无法叹气..为什么投票失败?寻找到底,然后得到位置。如果是零,那么文件是空的。是的,但是为什么我所做的不起作用,为什么发生了什么,发生了什么?@JoachimPileborg我试图使用我所理解的搜索到底,但它仍然不起作用,有什么提示吗?我不确定这到底是什么:/@Robolisk是一个可移植的库,具有良好的模板和功能,可以执行平台无关的任务,例如文件存在、空文件、文件大小等,网上有许多关于如何使用它们的示例,如下所示,其中显示了一些带有文件的库的示例ust检查它是否为空,它将始终为空?考虑到我想检查它是否已包含内容,然后决定如何处理它(在这种情况下,从用户处获取目标)。通过使用同步,然后执行检查,您建议这可能是我的解决方案?我不确定如何使用“同步”与我的代码情况一致。您发布的代码首先以非附加模式打开文件进行写入,这实际上会使文件为空。如果您想知道文件是否为空,请在打开文件进行写入之前对其进行测试。1/测试您是否可以打开文件,即文件是否存在;2/测试您是否处于eof而未读取,如果是,则文件为空s empty.OMG APPEND模式。这是所有问题的症结所在,谢谢idk我是如何忽略了这一点的。不过谢谢你的同步想法,这是一件我从未研究过的好事情
#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";
    }
}