在 C++ 中检查空文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2390912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:16:23  来源:igfitidea点击:

Checking for an empty file in C++

c++eof

提问by Crystal

Is there an easy way to check if a file is empty. Like if you are passing a file to a function and you realize it's empty, then you close it right away? Thanks.

有没有一种简单的方法来检查文件是否为空。就像如果你将一个文件传递给一个函数并且你意识到它是空的,然后你马上关闭它?谢谢。

Edit, I tried using the fseek method, but I get an error saying 'cannot convert ifstream to FILE *'.

编辑,我尝试使用 fseek 方法,但收到错误消息,提示“无法将 ifstream 转换为 FILE *”。

My function's parameter is

我的函数参数是

myFunction(ifstream &inFile)

回答by GManNickG

Perhaps something akin to:

也许类似于:

bool is_empty(std::ifstream& pFile)
{
    return pFile.peek() == std::ifstream::traits_type::eof();
}

Short and sweet.

简短而甜蜜。



With concerns to your error, the other answers use C-style file access, where you get a FILE*with specific functions.

考虑到您的错误,其他答案使用 C 样式文件访问,您可以在其中获得FILE*具有特定功能的文件。

Contrarily, you and I are working with C++ streams, and as such cannot use those functions. The above code works in a simple manner: peek()will peek at the stream and return, without removing, the next character. If it reaches the end of file, it returns eof(). Ergo, we just peek()at the stream and see if it's eof(), since an empty file has nothing to peek at.

相反,你和我正在使用 C++ 流,因此不能使用这些函数。上面的代码以一种简单的方式工作:peek()将查看流并返回,而不删除下一个字符。如果到达文件末尾,则返回eof(). 因此,我们只是peek()在流中看看它是否是eof(),因为一个空文件没有什么可窥视的。

Note, this also returns true if the file never opened in the first place, which should work in your case. If you don't want that:

请注意,如果文件从未打开过,这也会返回 true,这应该适用于您的情况。如果你不想这样:

std::ifstream file("filename");

if (!file)
{
    // file is not open
}

if (is_empty(file))
{
    // file is empty
}

// file is open and not empty

回答by pajton

Ok, so this piece of code should work for you. I changed the names to match your parameter.

好的,所以这段代码应该适合你。我更改了名称以匹配您的参数。

inFile.seekg(0, ios::end);  
if (inFile.tellg() == 0) {    
  // ...do something with empty file...  
}

回答by Mehrdad Afshari

Seek to the end of the file and check the position:

查找到文件末尾并检查位置:

 fseek(fileDescriptor, 0, SEEK_END);
 if (ftell(fileDescriptor) == 0) {
     // file is empty...
 } else {
     // file is not empty, go back to the beginning:
     fseek(fileDescriptor, 0, SEEK_SET);
 }

If you don't have the file open already, just use the fstatfunction and check the file size directly.

如果您还没有打开文件,只需使用该fstat功能并直接检查文件大小。

回答by Vytaute

use this: data.peek() != '\0'

使用这个: data.peek() != '\0'

I've been searching for an hour until finaly this helped!

我一直在寻找一个小时,直到最后这有帮助!

回答by user4471014

char ch;
FILE *f = fopen("file.txt", "r");

if(fscanf(f,"%c",&ch)==EOF)
{
    printf("File is Empty");
}
fclose(f);

回答by CroCo

How about (not elegant way though )

怎么样(虽然不是优雅的方式)

int main( int argc, char* argv[] )
{
    std::ifstream file;
    file.open("example.txt");

    bool isEmpty(true);
    std::string line;

    while( file >> line ) 
        isEmpty = false;

        std::cout << isEmpty << std::endl;
}

回答by sizzzzlerz

pFile = fopen("file", "r");
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
if (size) {
  fseek(pFile, 0, SEEK_SET);
  do something...
}

fclose(pFile)

回答by amna

if (nfile.eof()) // Prompt data from the Priming read:
    nfile >> CODE >> QTY >> PRICE;
else
{
    /*used to check that the file is not empty*/
    ofile << "empty file!!" << endl;
    return 1;
}