如何清除在C文件的全部内容? [英] How do I clear the whole contents of a file in C?

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

问题描述

我有一些user1的数据的文件。我想通过清除的文件的内容以用于用户2的同一个文件。

I have a file with some of user1's data. I want to use the same file for user2 by clearing the content of the file.

我的想法是,当一个新的用户来了,previous用户的数据应该是明确的和相同的文件应该是准备好新的用户。

My idea is that when a new user comes, data of the previous user should be clear and the same file should be ready for the new user.

推荐答案

由于@stefan使用说的fopen()与W模式将做的工作适合你。当你打开一个文件,W标志,它创造了书写一个空文件。如果具有相同名称的文件已经存在,它的内容被删除,该文件被视为空的新文件。

As @stefan said using fopen() with "w" mode will do the job for you. When you open a file with "w" flag it creates an empty file for writing. If a file with the same name already exists its contents are erased and the file is treated as an empty new file.

如果文件已经打开,你可以使用则freopen()功能从W模式stdio.h中,因为它会首先关闭该文件,然后重新打开写擦除无论是在文件previously。

If the file is already open you can use freopen() function from stdio.h with "w" mode as it will first close the file and then reopen it for writing erasing whatever was in the file previously.

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

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