读书人

linux上 直接打开然后在关闭文件

发布时间: 2012-09-16 17:33:16 作者: rapoo

linux下 ,直接打开,然后在关闭文件 fclose也出错
linux 下,c程序,就是简单的写日志,打开文件,写日志,关闭文件,但是每次关闭文件fclose都报错,错误信息如下:
*** glibc detected *** ./test: free(): invalid next size (normal): 0x0000000007be5620 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3888a71634]
/lib64/libc.so.6(cfree+0x8c)[0x3888a74c5c]
/lib64/libc.so.6(fclose+0x14b)[0x3888a6089b]
./test[0x40347e]
./test[0x408770]
./test[0x405e89]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x3888a1d8b4]
./test(__gxx_personality_v0+0xa9)[0x4017a9]
======= Memory map: ========
00400000-00411000 r-xp 00000000 fd:1e 6307883 /src/test
00610000-00611000 rw-p 00010000 fd:1e 6307883 /src/test
00611000-00612000 rw-p 00611000 00:00 0
07be4000-07c05000 rw-p 07be4000 00:00 0
3887a00000-3887a1a000 r-xp 00000000 fd:00 229676 /lib64/ld-2.5.so
3887c1a000-3887c1b000 r--p 0001a000 fd:00 229676 /lib64/ld-2.5.so
3887c1b000-3887c1c000 rw-p 0001b000 fd:00 229676 /lib64/ld-2.5.so
3888a00000-3888b4a000 r-xp 00000000 fd:00 229677 /lib64/libc-2.5.so
3888b4a000-3888d49000 ---p 0014a000 fd:00 229677 /lib64/libc-2.5.so
3888d49000-3888d4d000 r--p 00149000 fd:00 229677 /lib64/libc-2.5.so
3888d4d000-3888d4e000 rw-p 0014d000 fd:00 229677 /lib64/libc-2.5.so
3888d4e000-3888d53000 rw-p 3888d4e000 00:00 0
3888e00000-3888e82000 r-xp 00000000 fd:00 229681 /lib64/libm-2.5.so
3888e82000-3889081000 ---p 00082000 fd:00 229681 /lib64/libm-2.5.so
3889081000-3889082000 r--p 00081000 fd:00 229681 /lib64/libm-2.5.so
3889082000-3889083000 rw-p 00082000 fd:00 229681 /lib64/libm-2.5.so
3889600000-3889615000 r-xp 00000000 fd:00 229680 /lib64/libpthread-2.5.so
3889615000-3889814000 ---p 00015000 fd:00 229680 /lib64/libpthread-2.5.so
3889814000-3889815000 r--p 00014000 fd:00 229680 /lib64/libpthread-2.5.so
3889815000-3889816000 rw-p 00015000 fd:00 229680 /lib64/libpthread-2.5.so
3889816000-388981a000 rw-p 3889816000 00:00 0
388aa00000-388aa0d000 r-xp 00000000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388aa0d000-388ac0d000 ---p 0000d000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388ac0d000-388ac0e000 rw-p 0000d000 fd:00 229683 /lib64/libgcc_s-4.1.2-20080102.so.1
388ae00000-388aee6000 r-xp 00000000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388aee6000-388b0e5000 ---p 000e6000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0e5000-388b0eb000 r--p 000e5000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0eb000-388b0ee000 rw-p 000eb000 fd:09 950539 /usr/lib64/libstdc++.so.6.0.8
388b0ee000-388b100000 rw-p 388b0ee000 00:00 0
388ea00000-388ea15000 r-xp 00000000 fd:00 229701 /lib64/libnsl-2.5.so
388ea15000-388ec14000 ---p 00015000 fd:00 229701 /lib64/libnsl-2.5.so
388ec14000-388ec15000 r--p 00014000 fd:00 229701 /lib64/libnsl-2.5.so
388ec15000-388ec16000 rw-p 00015000 fd:00 229701 /lib64/libnsl-2.5.so
388ec16000-388ec18000 rw-p 388ec16000 00:00 0
2b145867a000-2b145867d000 rw-p 2b145867a000 00:00 0
2b14586a2000-2b14586a3000 rw-p 2b14586a2000 00:00 0
2b14589a5000-2b14589a9000 rw-p 2b14589a5000 00:00 0
2b145c000000-2b145c021000 rw-p 2b145c000000 00:00 0
2b145c021000-2b1460000000 ---p 2b145c021000 00:00 0
7fff5241a000-7fff52430000 rw-p 7fff5241a000 00:00 0 [stack]


ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vdso]
Aborted
程序如下:
void errLog(int level ,const char *fmt, ...)
{
FILE *fp;
va_list args;
char lsTmpBuf[FILE_NAME_LEN+1];
char lsStrBuf[STR_BUF_LEN+1];

initStr(lsTmpBuf);
initStr(lsStrBuf);

snprintf(lsTmpBuf, FILE_NAME_LEN, "%s/ERROR@%ld.log", ErrLogPath, getCurDate());
fp = fopen(lsTmpBuf, "a+");
if(fp == NULL)
{
printf("ERROR: open perror log file faild[%s][%s],check you path!\n", strerror(errno), lsTmpBuf);
exit(1);
}

va_start(args, fmt);
vsprintf(lsStrBuf, fmt, args);
va_end(args);
if(level==1)
{
fprintf(fp, "%s[%s]%s", getSystemTime(0), "WARN",lsStrBuf);
}
else
{
fprintf(fp, "%s[%s]%s", getSystemTime(0), "ERROR",lsStrBuf);
}
if(fp!=NULL)
fclose(fp);
}
后来干脆把中间写的注释掉,直接打开,然后就关闭,还是fclose就错误
fp = fopen(lsTmpBuf, "a+");
if(fp == NULL)
{
printf("ERROR: open perror log file faild[%s][%s],check you path!\n", strerror(errno), lsTmpBuf);
exit(1);
}
if(fp!=NULL)
fclose(fp);

这样也错,不知道为什么

[解决办法]
改用这个

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef WIN32    #include <windows.h>    #include <io.h>#else    #include <unistd.h>    #include <sys/time.h>    #include <pthread.h>    #define  CRITICAL_SECTION   pthread_mutex_t    #define  _vsnprintf         vsnprintf#endif//Log{#define MAXLOGSIZE 100000000#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))#include <time.h>#include <stdarg.h>char logfilename1[]="MyLog1.log";char logfilename2[]="MyLog2.log";char logstr[16000];char datestr[16];char timestr[16];char ms10[3];CRITICAL_SECTION cs_log;FILE *flog;int centisec() {#ifdef WIN32    return ((GetTickCount()%1000L)/10)%100;#else    struct timeval tv;    if (!gettimeofday(&tv,NULL)) {        return ((tv.tv_usec%1000000L)/10000)%100;    } else {        return 0;    }#endif}#ifdef WIN32void Lock(CRITICAL_SECTION *l) {    EnterCriticalSection(l);}void Unlock(CRITICAL_SECTION *l) {    LeaveCriticalSection(l);}#elsevoid Lock(CRITICAL_SECTION *l) {    pthread_mutex_lock(l);}void Unlock(CRITICAL_SECTION *l) {    pthread_mutex_unlock(l);}#endifvoid LogV(const char *pszFmt,va_list argp) {    struct tm *now;    time_t aclock;    if (NULL==pszFmt||0==pszFmt[0]) return;    if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;    time(&aclock);    now=localtime(&aclock);    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );    sprintf(ms10,"%02d",centisec());    printf("%s %s.%s %s",datestr,timestr,ms10,logstr);    flog=fopen(logfilename1,"a");    if (NULL!=flog) {        fprintf(flog,"%s %s.%s %s",datestr,timestr,ms10,logstr);        if (ftell(flog)>MAXLOGSIZE) {            fclose(flog);            if (rename(logfilename1,logfilename2)) {                remove(logfilename2);                rename(logfilename1,logfilename2);            }            flog=fopen(logfilename1,"a");            if (NULL==flog) return;        }        fclose(flog);    }}void Log(const char *pszFmt,...) {    va_list argp;    Lock(&cs_log);    va_start(argp,pszFmt);    LogV(pszFmt,argp);    va_end(argp);    Unlock(&cs_log);}//Log}int main(int argc,char * argv[]) {    int i;#ifdef WIN32    InitializeCriticalSection(&cs_log);#else    pthread_mutex_init(&cs_log,NULL);#endif    for (i=0;i<10000;i++) {        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);    }#ifdef WIN32    DeleteCriticalSection(&cs_log);#else    pthread_mutex_destroy(&cs_log);#endif    return 0;} 


[解决办法]
如果你确定已经打开了文件并未做任何操作就 fclose, 用

int ferror(FILE *stream)
  ferror returns non-zero if the error indicator for stream is set. 

void perror(const char *s)
perror(s) prints s and an implementation-defined error message corresponding to the integer in errno,

检查看看
[解决办法]
不懂 学习学习

读书人网 >C语言

热点推荐