c语言fclose什么作用,fclose()的源码是什么?那位高人能指点一下?解决思路

当前位置:我的异常网» C语言 » fclose()的源码是什么?那位高人能指点一下?解决思路

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

www.myexceptions.net  网友分享于:2013-04-03  浏览:30次

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

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值