清空文件 [英] emptying files

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

问题描述

我写了这个简短的程序来清空文件。它的效果很好

除了它截断。我的猜测是,它处于fopen模式

某个地方,但我玩过这个并且结果相同。空文件

零字节。如果我有一个512字节的数据文件,我想要512字节的''\ 0''。

原谅退出(1)的。

退出(EXIT_FAILURE)是我的实施方式。宏在我的实现中被定义为1.


比尔


/ * se,安全擦除* /

#include< stdio.h>

#include< stdlib.h>


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

{

if(argc!= 2){

puts(se usage error);

退出(1);

}

int i,j;

FILE * fo,* fw;

if((fo = fopen(argv [1]," ab"))== NULL){

printf("%i \ n",ferror(fo) );

clearerr(fo);

fclose(fo);

退出(1);

}

if((fw = fopen(argv [1]," ab"))== NULL){

printf("%i \ n", ferror(fw));

clearerr(fw);

fclose(fw);

退出(1);

}

while((i = getc(fo))!= EOF)

putc(j = 0,fw);

fclose(fo);

fclose(fw);

返回0;

}

解决方
在11月19日,上午12时26分,"比尔坎宁安" < nos ... @ nspam.invalidwrote:


原谅退出(1)的。

退出(EXIT_FAILURE)是我的实施方式。宏在我的实现中被定义为1.



Bill Cunningham关于可移植性。无价的




< vi ****** @ gmail.com在留言中写道

news:03 ********************************** @ w24g2000 prd.googlegroups.com ...


Bill Cunningham关于可移植性。无价



我只计划在我的系统上使用它。或者相信我会是

退出(EXIT_FAILURE);


Bill Cunningham写道:


我写了这个简短的程序来清空文件。它的效果很好

除了它截断。我的猜测是它处于fopen模式

某处



如果要将文件设置为全零,请确定多长时间它是,然后

fwrite很多零。不要一次尝试读取一个字节,设置

它为零,写回等等。


顺便说一下这不是''非常安全擦除。你需要在整个文件中依次写出至少

七个不同的位模式。


I have written this short program to empty out files. It works great
except that it truncates. My guess was that it was in the fopen mode
somewhere but I have played with that and the same results. Empty file of
zero bytes. If I have a 512 byte file of data, I want 512 bytes of ''\0''.
Pardon the exit(1)''s. It''s hort hand on my implementation for
exit(EXIT_FAILURE); The macro is defined on my implementation as 1.

Bill

/* se, secure erase */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
if (argc != 2) {
puts("se usage error");
exit(1);
}
int i, j;
FILE *fo, *fw;
if ((fo = fopen(argv[1], "ab")) == NULL) {
printf("%i\n", ferror(fo));
clearerr(fo);
fclose(fo);
exit(1);
}
if ((fw = fopen(argv[1], "ab")) == NULL) {
printf("%i\n", ferror(fw));
clearerr(fw);
fclose(fw);
exit(1);
}
while ((i = getc(fo)) != EOF)
putc(j = 0, fw);
fclose(fo);
fclose(fw);
return 0;
}

解决方案

On Nov 19, 12:26 am, "Bill Cunningham" <nos...@nspam.invalidwrote:

Pardon the exit(1)''s. It''s hort hand on my implementation for
exit(EXIT_FAILURE); The macro is defined on my implementation as 1.

Bill Cunningham on portability. Priceless



<vi******@gmail.comwrote in message
news:03**********************************@w24g2000 prd.googlegroups.com...

Bill Cunningham on portability. Priceless

I only plan to use this on my system. Or else believe me it would be
exit(EXIT_FAILURE);


Bill Cunningham wrote:

I have written this short program to empty out files. It works great
except that it truncates. My guess was that it was in the fopen mode
somewhere

If you want to set a file to all-zeros, determine how long it is, then
fwrite that many zeros into it. Don''t try to read a byte at a time, set
it to zero, write it back, etc etc.

By the way this isn''t a very secure erase. You''d need to write at least
seven different bitpatterns over the entire file sequentially.


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

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