首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎的关闭文件,并且不保存文件

2012-07-26 
怎样关闭文件,并且不保存文件C语言中“w”形式 fopen 一个文件,写了第一行了,发现不需要生成该文件,如果用fc

怎样关闭文件,并且不保存文件
C语言中“w”形式 fopen 一个文件,写了第一行了,发现不需要生成该文件,如果用fclose关闭这个文件,该文件也生成出来了,怎么样才能不让这个文件生成了呢?

[解决办法]
fopen执行完文件就已经被创建了,你不管调用不调用fclose都会被创建,如果你不需要这个文件可以在关闭后在删除。
[解决办法]
C语言中的Remove函数

简介:
  功 能: 删除一个文件,相当于unlink函数,但是如果它的filename参数是一个目录的话,其作用就相当于rmdir函数。
  用 法: int remove( const char *filename); remove函数图片头文件:在Visual C++ 6.0中可以用stdio.h也可以用io.h,前者更普遍。
  返回值:如果删除成功,remove返回0,否则返回EOF(-1)。
[解决办法]

探讨

引用:
不要老想的追求偏门方法 。
如果你的程序需要这个功能,,那就说明你应该改变下你的程序逻辑。(比如每次fwrite的地方,先在内存中保存,然后判断出确实要写入时,再写入文件 。)

问题别人已经fopen创建了文件,他问的问题是已经fopen了之后,而且已经写了一行了,才去做的判断,也就是说他在创建文件之前并不知道是否要创建这个文件。

[解决办法]
可以先写个临时文件,确定要了,改名,不需要就直接删。
[解决办法]
C/C++ code
NAME       remove - remove a file or directorySYNOPSIS       #include <stdio.h>       int remove(const char *pathname);DESCRIPTION       remove() deletes a name from the file system.  It calls unlink(2) for files, and rmdir(2) for directories.       If  the  removed  name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available       for reuse.       If the name was the last link to a file, but any processes still have the file open, the file will remain in existence until the  last  file  descriptor       referring to it is closed.       If the name referred to a symbolic link, the link is removed.       If the name referred to a socket, FIFO, or device, the name is removed, but processes which have the object open may continue to use it.RETURN VALUE       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.ERRORS       The errors that occur are those for unlink(2) and rmdir(2). 

热点排行