免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4944 | 回复: 10
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-05 11:07 |显示全部楼层 |倒序浏览
class A{
fstream fout;
}
class B{
setF( ? ){ fout = ?}
fstream fout;
}
要求将A中的fout传送给B中的fout.

论坛徽章:
0
2 [报告]
发表于 2007-01-05 11:22 |显示全部楼层
fstream &fout;

是否输入的时候也要带&
&fout << "test << endl;
还是fout << "test << endl;

论坛徽章:
0
3 [报告]
发表于 2007-01-05 11:34 |显示全部楼层
要在一个系统的所有类中传这个参数很麻烦。
我的目的是写日志。
请问高手,有没有好点的日志设计方法

论坛徽章:
0
4 [报告]
发表于 2007-01-05 11:35 |显示全部楼层
但是我又不想老是有打开该日志文件的操作。这样以提高性能。

论坛徽章:
0
5 [报告]
发表于 2007-01-05 11:39 |显示全部楼层
请高手多多指点!

论坛徽章:
0
6 [报告]
发表于 2007-01-05 11:45 |显示全部楼层
B(fstream &out) : fout(out) {}
构造函数中我还想初始化别的成员变量。该怎么办?

论坛徽章:
0
7 [报告]
发表于 2007-01-05 13:21 |显示全部楼层
fout << "** ** test......" << endl;
报错:Error: The type "std::basic_fstream<char, std::char_traits<char>>" is incomplete.
是什么原因呢?

论坛徽章:
0
8 [报告]
发表于 2007-01-09 12:56 |显示全部楼层

新问题:

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

论坛徽章:
0
9 [报告]
发表于 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
10 [报告]
发表于 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);
        
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP