fclose和close之间的区别 [英] Difference between fclose and close

查看:320
本文介绍了fclose和close之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我fopen一个文件,调用fcloseclose有什么区别,应该使用哪个文件?

If I fopen a file, what's the difference between calling fclose or close and which one should I use?

如果分叉的孩子也可以访问该文件,那么在完成文件操作后应该怎么做?

If forked children have access to the file as well, what should they do when they are finished with the file?

推荐答案

fclose()是与文件流相关的功能.在fopen()的帮助下打开文件并将流分配给FILE *ptr时.然后,您将使用fclose()关闭打开的文件.

fclose() is function related with file streams. When you open file with the help of fopen() and assign stream to FILE *ptr. Then you will use fclose() to close the opened file.

close()是与文件描述符相关的功能.当您使用open()打开文件并将描述符分配给int fd时.然后,您将使用close()关闭打开的文件.

close() is a function related with file descriptors. When you open file with the help of open() and assign descriptor to int fd. Then you will use close() to close the opened file.

fopen()fclose()等功能是 C标准功能,而open()close()等其他类别是POSIX特定的.这意味着用open()close()等编写的代码不是标准的C代码,因此不可移植.用fopen()fclose等编写的代码是标准代码,可以移植到任何类型的系统上.

The functions like fopen(), fclose() etc are C standard functions, while the other category of open(), close() etc are POSIX-specific. This means that code written with open(), close() etc is not a standard C code and hence non-portable. Whereas the code written with fopen(), fclose etc is a standard code and can be ported on any type of system.

我应该使用哪一个?

which one should I use?

这取决于您如何打开文件.如果使用fopen()打开文件,则应使用fclose();如果使用open()打开文件,则应使用close().

It depends on how you opened the file. If you open a file with fopen(), you should use fclose() and if you open file with open(), you should use close().

如果分叉的孩子也可以访问该文件,那么在完成文件操作后应该怎么做?

If forked children have access to the file as well, what should they do when they are finished with the file?

这还取决于您在调用fork()的位置:打开文件之前还是打开文件之后.

This is also dependent on where you made the fork() call: before opening the file or after opening it.

请参阅:在fork()ing时是否共享文件描述符吗?

请参阅:man fcloseman close

这篇关于fclose和close之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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