注册 登录
编程论坛 C++教室

ofstream使用求助

vfdff 发布于 2008-08-31 00:02, 1393 次点击
// VS2005 2008/8/24

#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;

struct data{
    unsigned type    :    2 ;
    unsigned d        :    1 ;
    unsigned context:    5 ;
};

void fprint(ofstream file,struct data t)
{
    int temp = (t.type<<6) + (t.d<<5) + t.context;
    if (temp<16)
    {
        file<<hex<<0 ;
    }
    file<<hex<<temp<<endl ;
}

int main(void)
{
    int j;
    ofstream fsOut("TestData.dat");
    
    for(j=0;j<16;j++)
    {
        int j1=(j&8)&&1,j2=(j&4)&&1,j3=(j&2)&&1,j4=j&&1;
        fsOut<<hex<<j<<" "<<j<<" "<<dec<<1<<endl ;
    }

    struct data t;
    t.type = 2;
    t.d = 0;
    t.context = 0x12;
    fprint(fsOut,t);
    fsOut.close();
    cout<<hex<<0x12<<endl ;
    return 0;
}

上面得子函数 fprint()定义的由什么问题呢 ?
为什么运行的时候出现这个错误
但是的哦dos窗口能输出 12 ,说明这个错误发生在 cout<<hex<<0x12<<endl ;语句之后,
而这个语句之后只有个return
7 回复
#2
随心2008-08-31 02:28
iostream不允许复制,传引用就可以了
程序代码:

void fprint(ofstream &file,struct data t)
{
    int temp = (t.type<<6) + (t.d<<5) + t.context;
    if (temp<16)
    {
        file<<hex<<0 ;
    }
    file<<hex<<temp<<endl ;
}
#3
随心2008-08-31 02:30
顺便说一下,你应该把编译器警告等级量调高
#4
vfdff2008-08-31 08:08
回复 2# 随心 的帖子
谢谢,还确实如此
#5
blueboy820062008-08-31 08:27
回复 3# 随心 的帖子
请问一下...不知道,要怎么设置才能提高?
#6
vfdff2008-08-31 09:37
回复 5# blueboy82006 的帖子
project-->setting-->c/c++中
#7
南国利剑2010-05-28 00:07
二楼说的不错。
#8
南国利剑2010-06-01 01:18
谁举报的这个主题?
有什么问题吗?
1