如何在C中检查文件是否为空? [英] How do I check if a file is empty in C??

查看:105
本文介绍了如何在C中检查文件是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我正在写一个播放列表管理协议,我有一个

文件,其中包含所有播放列表和一个包含所有

歌曲的文件。 ..在创建播放列表之前我需要检查

播放列表文件是否为空,这样我就可以为

播放列表id字段分配一个整数值是写入

档案的第一个播放列表....任何人都可以帮忙吗?提前致谢


wetherbean

Hi group..I am writing a playlist management protocol where I have a
file that holds all the playlists and a file that holds all the
songs....before a playlist is created I need to check to see if the
playlist file is empty so that I can assign an integer value to a
playlist id field if it is the first playlist being written to the
file....can anyone help?? Thanks in advance

wetherbean

推荐答案

没关系,有人刚刚给我一个想法我认为会工作

罚款...他说fseek他的文件结束然后做一个ftell和

如果它是0那么文件是空的

Nevermind, someone just gave me an idea that I think will work
fine...he said to fseek tot he end of the file and then do an ftell and
if it is 0 then the file is empty


在文章< 11 ********************* @ o13g2000cwo.googlegroups中。 com>

wetherbean< bj ****** @ gmail.com>写道:
In article <11*********************@o13g2000cwo.googlegroups. com>
wetherbean <bj******@gmail.com> wrote:
嗨组...我正在写一个播放列表管理协议,我有一个
文件,其中包含所有播放列表和一个包含所有歌曲的文件... 。在创建播放列表之前,我需要检查
播放列表文件是否为空,这样我就可以为
播放列表id字段分配一个整数值,如果它是第一个写入<播放列表的播放列表br /> fileja可以帮助任何人?提前致谢
Hi group..I am writing a playlist management protocol where I have a
file that holds all the playlists and a file that holds all the
songs....before a playlist is created I need to check to see if the
playlist file is empty so that I can assign an integer value to a
playlist id field if it is the first playlist being written to the
file....can anyone help?? Thanks in advance




定义:文件为空。如果你不能从中读到任何东西。

(这个定义中是否存在潜在的缺陷?[我说是的,但是

这是一个练习;你应该识别潜在的缺陷,

不仅回答是或不。:-)])


结论:打开文件并尝试阅读从中。如果你得到一些数据,那么它就不是空的。如果你立刻得到EOF,那就是空白。

-

现实生活中:风河系统的Chris Torek />
美国犹他州盐湖城(40°39.22''N,111°50.29''W)+1 801 277 2603

电子邮件:忘了它 http://web.torek.net/torek/index.html
阅读电子邮件就像在垃圾邮件中搜索食物一样,感谢垃圾邮件发送者。



Definition: a file is "empty" if you cannot read anything from it.
(Is there a potential flaw in this definition? [I say yes, but
this is an exercise; you are supposed to identify potential flaws,
not just answer "yes" or "no". :-) ])

Conclusion: open the file and try to read from it. If you get
some data, it is not empty. If you get EOF immediately, it is
empty.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


wetherbean写道:
wetherbean wrote:
没关系,有人只是给了我一个想法,我认为会工作好......他说fseek他的文件结束然后做一个ftell和
如果它是0然后文件是空的
Nevermind, someone just gave me an idea that I think will work
fine...he said to fseek tot he end of the file and then do an ftell and
if it is 0 then the file is empty




或者,如果你想避免实际打开每个文件,

直到你确信它是非空的,那么你可以使用stat()。

例如,看看下面的代码。


一个警告,如果你使用stat(),你会发现stat()调用时的文件大小为
。因此,原则上,

文件可能会在您实际打开之前被截断或删除,并且用它来做任何事情。

任何东西。


-Dan


get_file_size.c

----------------- -----------------------------------------

# include< stdio.h>

#include< sys / stat.h>

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

int i;


if(argc == 1){

fprintf(stderr," Usage:\ n \t%s(file1)(file2)..... \ n",argv [0]);

返回-1;

}


for(i = 1; i< argc; i ++){

struct stat stat_record;

printf("%s - ",argv [i]);

if(stat(argv [i],& stat_record))

printf(" Can''t stat(文件是否存在?)\ n");

else if(!stat_record.st_size)printf(" Empty file\\\
);

else {

/ *现在你可以打开文件了做你想做的事情

做 - 因为它是非空的

(或技术上更正确可能仍然非空)* /

printf("非空文件(%d字节)\ n",stat_record.st_size);

}

}

返回0;

}

----------------------------------- -----------------------



Alternatively, if you want to avoid actually having to open each file,
until you''re confident that it''s non-empty, then you could use stat().
As an example, check out the code below.

One caveat, if you use stat(), you''ll find out the size of the file at
the time of the stat() call. So, in principle, it''s possible for the
file to get truncated or deleted before you actually open it up and do
anything with it.

-Dan

get_file_size.c
----------------------------------------------------------
#include <stdio.h>
#include <sys/stat.h>

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

if (argc == 1) {
fprintf(stderr, "Usage:\n\t%s (file1) (file2) .....\n", argv[0]);
return -1;
}

for (i = 1; i < argc; i++) {
struct stat stat_record;
printf("%s - ", argv[i]);
if (stat(argv[i], &stat_record))
printf("Can''t stat (does file exist?)\n");
else if (!stat_record.st_size) printf("Empty file\n");
else {
/* Now you can open up the file an do what ever it is your going
to do - since it is non-empty
(or more technically correct probably still non-empty) */
printf(" Non-empty file (%d bytes)\n", stat_record.st_size);
}
}
return 0;
}
----------------------------------------------------------


这篇关于如何在C中检查文件是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆