如何在C程序中删除文件? [英] How to remove a file in C program?

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

问题描述

如何关闭文件并将其删除?

How do I close a file and remove it?

我有以下代码:

FILE *filePtr = fopen("fileName", "w");
...

现在我要关闭filePtr并删除文件"fileName".

Now I want to close filePtr and remove the file "fileName".

我应该:

fclose(filePtr);
remove("fileName");

或者:

remove("fileName");
fclose(filePtr);

我先做什么有关系吗?

谢谢!

推荐答案

这取决于操作系统.在* nix上,删除打开的文件会使其保持打开状态,并将数据保留在磁盘上,但会从文件系统中删除文件名,而实际上会在关闭时删除该文件;其他某些操作系统可能根本不允许您删除打开的文件.因此,建议使用前者以获得最大的可移植性.

That is OS-dependent. On *nix, deleting an open file leaves it open and the data on disk, but removes the filename from the filesystem, and actually deletes the file on close; some other operating systems may not let you delete an open file at all. Therefore the former is recommended for maximum portability.

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

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