免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: wys0436
打印 上一主题 下一主题

fstream 如何当参数传 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-01-09 12:56 |只看该作者

新问题:

由于是多进程操作,但在写流文件的时候报错。
打开文件方式:fout.open(bugname, ios:ut | ios::app );
在fout << "test" << endl;
的时候报错。
哪位碰到过这种问题,请指点一下。

论坛徽章:
0
12 [报告]
发表于 2007-01-09 15:20 |只看该作者
原帖由 wys0436 于 2007-1-9 12:56 发表
由于是多进程操作,但在写流文件的时候报错。
打开文件方式:fout.open(bugname, ios:ut | ios::app );
在fout << "test" << endl;
的时候报错。
哪位碰到过这种问题,请指点一下。

你怎么用的?跟前面不一致啊
fout是引用吗?
贴下稍微完整点的代码吧

论坛徽章:
0
13 [报告]
发表于 2007-01-10 14:19 |只看该作者
#ifndef Write_Log_h
#define Write_Log_h

#include <iostream.h>
#include <fstream.h>
#include <time.h>
#include <sys/timeb.h>

using namespace std;

class WriteLog
{
public:
        WriteLog( );
        ~WriteLog();
        bool Init();
        void PrintLog( char* buf );
        void SetLogFlag( int value );
        fstream fout;
private:
        char    fileName[200];
        char    cHour[3];
        char    cTime[11];
        int     iLogFlag;

};

#endif

论坛徽章:
0
14 [报告]
发表于 2007-01-10 14:22 |只看该作者
#include "find/WriteLog.h"
#include <pthread.h>

extern string  bugpath;
pthread_mutex_t    Log_mutex=PTHREAD_MUTEX_INITIALIZER;

WriteLog::WriteLog( )
{
        iLogFlag = 0;
        time_t          tt;
        struct tm       *tm;
        tt = time(NULL);
        tm = localtime(&tt);
        strftime(cTime,sizeof(cTime),"%Y-%m-%d\0",tm);
        strftime(cHour,sizeof(cHour),"%H\0",tm);
        memset( fileName, 0, sizeof(fileName) );
}

WriteLog::~WriteLog()
{
        if ( fout.is_open() )
                fout.close();
}

void WriteLog::SetLogFlag( int value )
{
        iLogFlag = value;
}

bool WriteLog::Init()
{
        sprintf( fileName, "%test_%s_%d.log.%s", bugpath.data(), cTime, iLogFlag, cHour );
        fout.open(fileName, ios:ut | ios::app);
        if ( !fout.is_open() )
        {
                cerr << "Can't open" << fileName << " file for output" << endl;
                return false;
        }
        return true;
}
oid WriteLog:rintLog( char* buf )
{
        if ( !fout.is_open() )
        {
                fout.open(fileName, ios:ut | ios::app);
                if ( !fout.is_open() )
                {
                        cerr << "Can't open" << fileName << " file for output" << endl;
                }
        }
        char            op_hour[3], op_day[11];
        time_t          tt;
        struct tm       *tm;
        tt = time(NULL);
        tm = localtime(&tt);
        strftime(op_day, sizeof(op_day), "%Y-%m-%d\0", tm );
        strftime(op_hour, sizeof(op_hour), "%H\0", tm );

        if ( strcmp(cHour, op_hour) != 0 )
        {
                fout.close();
                strcpy( cHour, op_hour );
                strcpy( cTime, op_day );
                sprintf( fileName, "%test_%s_%d.log", bugpath.data(), cTime, iLogFlag, cHour );
                fout.open(fileName, ios:ut | ios::app);
                if ( !fout.is_open() )
                {
                        cerr << "Can't open" << fileName << " file for output" << endl;
                }
        }

        pthread_mutex_lock(&Log_mutex);
        fout <<  buf;
        pthread_mutex_unlock(&Log_mutex);
        
}

论坛徽章:
0
15 [报告]
发表于 2007-01-10 14:23 |只看该作者
在PrintLog的时候报错。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP