当前位置:我的异常网» 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
文章评论