首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于C++ 在linux下面文件流的有关问题 大牛

2012-03-06 
关于C++ 在linux下面文件流的问题 求助大牛现在有一个CLog负责日志的写入工作代码如下头文件如下C/C++ cod

关于C++ 在linux下面文件流的问题 求助大牛

现在有一个CLog负责日志的写入工作代码如下

头文件如下

C/C++ code
#ifndef __LOG_H__#define __LOG_H__#include<cstdarg>#include<string>#include"nfx.h"#include"type.h"#define CLOG_LEVEL_INFO 1#define CLOG_LEVEL_NOTICE 2#define CLOG_LEVEL_FAIL 4#define CLOG_LEVEL_DEBUG 8#define CLOG_LEVEL_CIRCULAR_BUFFER 16#define CLOG_LEVEL_MUTEX_LOCKER 32#define CLOG_LEVEL_ALL 63extern const U32 DefLogLevel;class CLog{  public:    CLog(  );    CLog(char * threadName);    virtual ~CLog();    void Log(U32 level,const char * title,...);    void Logif(U32 level,bool condition,const char * title,...);  private:    void init( void );    void formatPrint(const char *title,va_list args );    void getThreadName( char* threadName );  private:    const U32 m_tid;};#endif


源文件如下

C/C++ code
#include "nfx.h"//#ifdef LINUX_PLATFORM#include <unistd.h>#include <pthread.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <stdlib.h>//#endif#include <stdio.h>#include <string>#include <fstream>#include <iostream>#include <string.h>#include "Log.h"using namespace std;/******************************************************//**/const U32 DefLogLevel = CLOG_LEVEL_ALL;/******************************************************/const char* LogFileDir = "./Log";void CLog::init( void ){    }CLog::CLog( void ):m_tid(pthread_self(  )){    this->init(  );}void CLog::getThreadName( char* threadName ){    DIR* dp;    struct dirent* entry;    struct stat statbuf;        if( ( dp = opendir( LogFileDir ) ) == NULL )    {        fprintf( stderr,"cannot open directory \n");        return;    }        //chdir( dir );        while( ( entry = readdir( dp ) )!=NULL )    {        lstat( entry->d_name,&statbuf );        if( !S_ISDIR( statbuf.st_mode ) )        {            fstream logfile;            logfile.open( entry->d_name,ifstream::in );            logfile.seekg(0,fstream::beg);                                    string titlebuf;                        cout<<"===============filename :"<<entry->d_name<<"====================="<<endl;                        while(logfile>>titlebuf)            {                cout<<titlebuf<<endl;                }            fstream::iostate oldstate = logfile.rdstate(  );            logfile.close();        }       }}CLog::CLog( char * threadName ):m_tid(pthread_self(  )){    this->init(  );        getThreadName( threadName );        }CLog::~CLog(){    }void CLog::formatPrint(const char *title,va_list args ){    FILE * filestream = fopen("./Log/DebugLog","a");    fprintf(filestream,"\n<===FILE : %s LINE : %d DATA : %s TIME : %s===>\n",__FILE__,__LINE__,__DATE__,__TIME__);    vfprintf(filestream,title,args);    fprintf(filestream,"\n");    fclose(filestream);}void CLog::Log(U32 level,const char * title,...){    if(DefLogLevel & level)    {        va_list arg_ptr;        va_start( arg_ptr,title );        this->formatPrint(title, arg_ptr );        va_end( arg_ptr );    }}void CLog::Logif( U32 level,bool condition,const char * title,... ){    if(condition && (DefLogLevel & level))    {        va_list arg_ptr;        va_start( arg_ptr,title );        this->formatPrint( title,arg_ptr );        va_end( arg_ptr );    }}



在第一次执行的时候 fstream::iostate oldstate = logfile.rdstate( ); 这里设定断点
发现oldstate为 failbit 我去 这是神马情况啊 谁遇到过

------解决方案--------------------


读取和写入采用不同的方式,代码风格比较乱。
建议统一文件操作方式。

热点排行