fopen和close

latachong 2008-08-05 12:16:31
fopen()打开的文件,不用fclose()关闭,而用close()关闭,会有什么问题么
...全文
281 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
latachong 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hqin6 的回复:]
如果用 close 来关闭一个缓冲文件,其行为是未定义的。因为 close 并不能保证刷新缓存,所以 fclose != close,不过可以这样认为:fclose = fflush + close。

[/Quote]

你的意思是,在linux系统下,close不能让缓存中的内容写到文件中去,而只是释放文件所占用的资源么?
太乙 2008-08-05
  • 打赏
  • 举报
回复
如果用 close 来关闭一个缓冲文件,其行为是未定义的。因为 close 并不能保证刷新缓存,所以 fclose != close,不过可以这样认为:fclose = fflush + close。
cad_vc 2008-08-05
  • 打赏
  • 举报
回复
编译的时候就应该抱错
太乙 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 latachong 的回复:]
引用楼主 latachong 的帖子:
fopen()打开的文件,不用fclose()关闭,而用close()关闭,会有什么问题么


我的意思是比如
FILE* fp = fopen("temp.txt"."a+");
int fd = fileno(fp);
close(fd);


FILE* fp = fopen("temp.txt"."a+");
fclose(fp);
的区别。。。
[/Quote]

在非linux系统下,使用fopen打开的文件
使用close关闭(注意,不是fclose),缓冲也相应关闭;

而在linux系统下,如果使用close关闭,结果缓冲还残留在内存中.
latachong 2008-08-05
  • 打赏
  • 举报
回复
楼上两位,不好意思,开始没表达明白。。。
太乙 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xkyx_cn 的回复:]
关闭不了

open返回值、close的参数类型是文件描述符 int型的

fopen fclose则是FILE* 类型的
[/Quote]

正解!
latachong 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 latachong 的帖子:]
fopen()打开的文件,不用fclose()关闭,而用close()关闭,会有什么问题么
[/Quote]

我的意思是比如
FILE* fp = fopen("temp.txt"."a+");
int fd = fileno(fp);
close(fd);


FILE* fp = fopen("temp.txt"."a+");
fclose(fp);
的区别。。。
xkyx_cn 2008-08-05
  • 打赏
  • 举报
回复
关闭不了

open返回值、close的参数类型是文件描述符 int型的

fopen fclose则是FILE* 类型的
realdragon2 2008-08-05
  • 打赏
  • 举报
回复
http://blog.chinaunix.net/u/21067/showart_213410.html
btw: 楼主这样分配分数也太不公平了,呵呵. 其他人还怎么给你答题.
latachong 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hqin6 的回复:]
引用 8 楼 latachong 的回复:
引用 7 楼 hqin6 的回复:
如果用 close 来关闭一个缓冲文件,其行为是未定义的。因为 close 并不能保证刷新缓存,所以 fclose != close,不过可以这样认为:fclose = fflush + close。



你的意思是,在linux系统下,close不能让缓存中的内容写到文件中去,而只是释放文件所占用的资源么?


既然 close 用 fopen 打开的文件是一种未定义行为,就是说标准没有规定这样会导致什么行为,所以…
[/Quote]

基本上明白了。。。非常感谢
太乙 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 latachong 的回复:]
引用 7 楼 hqin6 的回复:
如果用 close 来关闭一个缓冲文件,其行为是未定义的。因为 close 并不能保证刷新缓存,所以 fclose != close,不过可以这样认为:fclose = fflush + close。



你的意思是,在linux系统下,close不能让缓存中的内容写到文件中去,而只是释放文件所占用的资源么?
[/Quote]

既然 close 用 fopen 打开的文件是一种未定义行为,就是说标准没有规定这样会导致什么行为,所以结果导致缓冲刷新也罢不刷新也罢,这样的行为都是合法的,编译器可以自由实现。
zjw6861982 2008-08-05
  • 打赏
  • 举报
回复
不匹配,不要再搅和了
aozhi 2008-08-05
  • 打赏
  • 举报
回复
不是匹配的
fclose需要文件指针,而close需要文件句柄。
太乙 2008-08-05
  • 打赏
  • 举报
回复
The function

int fclose(FILE *fp)

is the inverse of fopen, it breaks the connection between the file pointer and the external name that was established by fopen, freeing the file pointer for another file. Since most operating systems have some limit on the number of files that a program may have open simultaneously, it's a good idea to free the file pointers when they are no longer needed, as we did in cat. There is also another reason for fclose on an output file - it flushes the buffer in which putc is collecting output. fclose is called automatically for each open file when a program terminates normally. (You can close stdin and stdout if they are not needed. They can also be reassigned by the library function freopen.)
lovexieli 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hqin6 的回复:]
如果用 close 来关闭一个缓冲文件,其行为是未定义的。因为 close 并不能保证刷新缓存,所以 fclose != close,不过可以这样认为:fclose = fflush + close。
[/Quote]

有没有例子!

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧