C语言 如何使用C检查文件是否有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13566082/
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
How to check if a file has content or not using C?
提问by Oleksandr Kravchuk
I have a source file file1and a destination file file2, here I have to move content from file1to file2.
我有一个源文件file1和目标文件file2,在这里我必须从移动内容 file1来file2。
So I have to do some validation first.
所以我必须先做一些验证。
I must check source file is existing or not? I can check using this:
fp = fopen( argv[1],"r" ); if ( fp == NULL ) { printf( "Could not open source file\n" ); exit(1); }Then I have to check if the source file has any content or not? If it is empty, I have to throw some error message.
我必须检查源文件是否存在?我可以使用这个检查:
fp = fopen( argv[1],"r" ); if ( fp == NULL ) { printf( "Could not open source file\n" ); exit(1); }然后我必须检查源文件是否有任何内容?如果它是空的,我必须抛出一些错误消息。
This is what I've tried until the moment.
这是我到目前为止所尝试的。
回答by Oleksandr Kravchuk
回答by Mike
You can do this withoutopening the file as well using the statmethod.
您也可以使用该方法在不打开文件的情况下执行此操作stat。
#include <sys/stat.h>
#include <errno.h>
int main(int argc, char *argv[])
{
struct stat stat_record;
if(stat(argv[1], &stat_record))
printf("%s", strerror(errno));
else if(stat_record.st_size <= 1)
printf("File is empty\n");
else {
// File is present and has data so do stuff...
}
So if the file doesn't exist you'll hit the first ifand get a message like: "No such file or directory"
因此,如果该文件不存在,您将点击第一个if并收到如下消息:"No such file or directory"
If the file exists and is empty you'll get the second message "File is empty"
如果文件存在且为空,您将收到第二条消息 "File is empty"
This functionality exists on both Linux and Windows, but on Win it's _stat. I haven't tested the windows code yet, but you can see examples of it here.
此功能在 Linux 和 Windows 上都存在,但在 Win 上它是_stat. 我尚未测试 Windows 代码,但您可以在此处查看示例。
回答by Ivaylo Strandjev
回答by Olaf Dietsche
Just look if there's a character to read
看看有没有要读的字符
int c = fgetc(fp);
if (c == EOF) {
/* file empty, error handling */
} else {
ungetc(c, fp);
}
回答by Ivan Ignatiev
fseek(fp, 0, SEEK_END); // goto end of file
if (ftell(fp) == 0)
{
//file empty
}
fseek(fp, 0, SEEK_SET); // goto begin of file
// etc;
reference for ftell and example
回答by MOHAMED
you can check if the file size > 0
您可以检查文件大小是否> 0
after your code of checking file exist (before you close the file) you add the following code
在检查文件的代码存在后(在关闭文件之前),您添加以下代码
size = 0
if(fp!=NULL)
{
fseek (fp, 0, SEEK_END);
size = ftell (fp);
rewind(fp);
}
if (size==0)
{
// print your error message here
}
回答by yageek
It is painful to open the data and count each byte of the file. It is better to ask to the operating system to give you details about the files you want to used. The API relies depends on your operating system as Mike previously said.
打开数据并计算文件的每个字节很痛苦。最好要求操作系统提供有关要使用的文件的详细信息。正如 Mike 之前所说,API 依赖于您的操作系统。
回答by Dvir Hatuka
you can use the feof()function.
example:
您可以使用该feof()功能。例子:
if(feof(file))
{
printf("empty file\n");
}

