如何检测空文件? [英] How to detect an empty file?

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

问题描述

亲爱的,


我认为代码

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

pt_fichier_probleme = fopen(nom_fichier," w");


if(pt_fichier_probleme == NULL){< br $>
message_warning_s

(Erreur l''ouverture du fichier \ n%s \ n,(gchar *)nom_fichier);

return;}

else {

rewind(pt_fichier_probleme); / *确保我们开始* /

if(feof(pt_fichier_probleme)== 0){

/ *我们不在缓冲区的末尾..这意味着

文件已经有了一些内容! * /

if(AskConfirmation(user_data)== 0){

/ * L''utilisateur ne veut pas qu''on ecrive sur le fichier !! * /

fclose(pt_fichier_probleme);

返回;};

};};

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


是一个很好的方式

- - 打开文件nom_fichier进行写作,

- 如果不可能则检测到错误,

- 如果文件不为空,请询问

用户是否仍想覆盖它。

(即'AskConfirmation:带问题的窗口等等)

事实证明,确认总是被问到:-(


帮助?

最好!

Amities,

Olivier

解决方案



Olivier写道:


亲爱的全部,


我认为代码



< snip>


加,但......只是使用stat()。它会告诉你


a)如果文件或目录存在

b)它是什么类型的条目[文件,目录,符号链接,管道等]

c)大小


和平!


汤姆


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

Tom St Denis< to ******** @ gmail.comwrote:


> OT但是...只需使用stat()。它将告诉你


> a)如果文件或目录存在
b)它是什么类型的条目[文件,目录,符号链接,管道等]
c)大小



stat()不是标准C的一部分。目录,符号链接,

pipe,etc。

-

有些想法错了,只有一个非常聪明的人

可以相信他们。 - George Orwell


Olivier写道:


亲爱的,


我以为代码

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

pt_fichier_probleme = fopen(nom_fichier," w");



在w中打开文件模式截断

如果fopen成功,文件为零长度,

所以之后不需要检查文件大小。


也许你想在r +中打开文件

模式?


if(pt_fichier_probleme == NULL){

message_warning_s

(&Erreur l'' ouverture du fichier \ n%s \ n",(gchar *)nom_fichier);

return;}

else {

rewind (pt_fichier_probleme); / *确保我们开始* /

if(feof(pt_fichier_probleme)== 0){

/ *我们不在缓冲区的末尾..这意味着

文件已经有了一些内容! * /

if(AskConfirmation(user_data)== 0){

/ * L''utilisateur ne veut pas qu''on ecrive sur le fichier !! * /

fclose(pt_fichier_probleme);

返回;};

};};

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


是一个很好的方式

- - 打开文件nom_fichier进行写作,

- 如果不可能则检测到错误,

- 如果文件不为空,请询问

用户是否仍想覆盖它。

(即'AskConfirmation:带问题的窗口等等)

事实证明,总是要求确认:-(



用''fseek确定文件开头和结尾之间文件位置的差异

''和''ftell''。


-

希望这会有所帮助,

史蒂文


Dear all,

I thought the code
-----------------------------
pt_fichier_probleme = fopen(nom_fichier, "w");

if(pt_fichier_probleme == NULL){
message_warning_s
("Erreur l''ouverture du fichier\n%s\n", (gchar *)nom_fichier);
return;}
else {
rewind(pt_fichier_probleme); /* Be sure we''re at beginning */
if(feof(pt_fichier_probleme) == 0){
/* We are not at end of buffer ... It means the
file already has some content!! */
if( AskConfirmation(user_data) == 0){
/* L''utilisateur ne veut pas qu''on ecrive sur le fichier !!*/
fclose(pt_fichier_probleme);
return;};
};};
-----------------------------

was an excellent way of
-- opening the file nom_fichier for writing,
-- detecting a mistake if it was not possible,
-- if the file was not empty, the askign the
user whether it still wants to overwrite it.
(that''s AskConfirmation : a window with the question and so on)
As it turns out, confirmation is always asked :-(

Help?
Best !
Amities,
Olivier

解决方案


Olivier wrote:

Dear all,

I thought the code

<snip>

OT but ... just use stat(). It will tell you

a) if the file or directory exists
b) What sort of entry is it [file, directory, symlink, pipe, etc]
c) The size

Peace!

Tom


In article <11*********************@b28g2000cwb.googlegroups. com>,
Tom St Denis <to********@gmail.comwrote:

>OT but ... just use stat(). It will tell you

>a) if the file or directory exists
b) What sort of entry is it [file, directory, symlink, pipe, etc]
c) The size

stat() is not part of standard C. Neither are directories, symlinks,
pipes, "etc".
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell


Olivier wrote:

Dear all,

I thought the code
-----------------------------
pt_fichier_probleme = fopen(nom_fichier, "w");


Opening the file in "w" mode truncates
the file to zero length if fopen was successful,
so there''s no need to check for file size afterward.

Perhaps you were thinking of opening the file
in "r+" mode?

if(pt_fichier_probleme == NULL){
message_warning_s
("Erreur l''ouverture du fichier\n%s\n", (gchar *)nom_fichier);
return;}
else {
rewind(pt_fichier_probleme); /* Be sure we''re at beginning */
if(feof(pt_fichier_probleme) == 0){
/* We are not at end of buffer ... It means the
file already has some content!! */
if( AskConfirmation(user_data) == 0){
/* L''utilisateur ne veut pas qu''on ecrive sur le fichier !!*/
fclose(pt_fichier_probleme);
return;};
};};
-----------------------------

was an excellent way of
-- opening the file nom_fichier for writing,
-- detecting a mistake if it was not possible,
-- if the file was not empty, the askign the
user whether it still wants to overwrite it.
(that''s AskConfirmation : a window with the question and so on)
As it turns out, confirmation is always asked :-(

Determine the difference in file position between
the start and end of the file with ''fseek'' and ''ftell''.

--
Hope this helps,
Steven


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

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