专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C语言

fclose()的源码是什么?那位高人能指点一下?解决思路

发布时间:2011-06-28 16:12:54 文章来源:www.iduyao.cn 采编人员:星星草
fclose()的源码是什么?那位高人能指点一下?

现在发现一个错误,可能是由于打开一个文件,而关闭两次造成了,苦于不知道fclose()的内部实现,哪位能指点一下?十分感激!

------解决方案--------------------
steedhorse(晨星)说的一点没错,而且光有fclose源码也没有,要看FILE的定义及其它相关函数
以下为small C的fclose源码:
extern int Ustatus[], Udevice[], Ufd[];
fclose(fd) int fd; {
if(!Umode(fd)) return (ERR);
if(!isatty(fd)) {
if(Umsdos(0,0,Ufd[fd],CLOFIL)==ERR)
return (ERR);
}
return (Ustatus[fd]=Udevice[fd]=NULL);
}

以下是C++ BUILDER的:
int _RTLENTRY _EXPFUNC fclose (FILE *fp)
{
int rc;

if (fp == NULL || fp-> token != (unsigned char) fp)
return(EOF); /* invalid pointer */

_lock_stream(fp);

if (fp-> bsize)
{
if (fp-> level < 0 && fflush (fp))
RETURN(EOF);

if (fp-> flags & _F_BUF)
free (fp-> buffer);
}

rc = __close (fp-> fd);

fp-> flags = 0;
fp-> bsize = 0;
fp-> level = 0;
fp-> fd = -1;

if (fp-> istemp != 0)
{
_unlink(__mkname((char *)NULL, (char *)NULL, fp-> istemp));
fp-> istemp = 0;
}

exit:
_unlock_stream(fp);
return rc;
}

------解决方案--------------------
#ifdef _MT

int __cdecl fclose (
FILE *stream
)
{
int result = EOF;

_ASSERTE(stream != NULL);

/* If stream is a string, simply clear flag and return EOF */
if (stream-> _flag & _IOSTRG)
stream-> _flag = 0; /* IS THIS REALLY NEEDED ??? */

/* Stream is a real file. */
else {
_lock_str(stream);
result = _fclose_lk(stream);
_unlock_str(stream);
}

return(result);
}
int __cdecl _fclose_lk (
FILE *str
)
{
REG1 FILE *stream;
REG2 int result = EOF;

/* Init near stream pointer */
stream = str;

#else /* _MT */

int __cdecl fclose (
FILE *str
)
{
REG1 FILE *stream;
REG2 int result = EOF;

/* Init near stream pointer */
stream = str;

if (stream-> _flag & _IOSTRG) {
stream-> _flag = 0;
return(EOF);
}

#endif /* _MT */

_ASSERTE(str != NULL);

if (inuse(stream)) {

/* Stream is in use:
(1) flush stream
(2) free the buffer
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: