老弟,我了曾为这样的事伤透了脑筋,我深有体会。
现在就告诉你解决的方法:
在 工程-》设置-》C/C++中的 分类中选取Code Generation项,再将Struct member alignment中的字节数设为你结构中最小的字节数就可以了。
根据你现在的情况应设为2Byte即可。

解决方案 »

  1.   

    就是二进制的问题,我也预见过
    用这个格式就行了
    stream = fopen( szFileName, "rb" )) 0D 0A明显是文本格式的回车换行符嘛。
      

  2.   

    我是以二进制方式打开,使用的是 ofstream。open(filename,ios::out|ios::app) 和iftream,open(filename,ios::in)
      

  3.   

    改成ofstream.open(filename, ios::out|ios:app|ios:binary)和ifstream.open(filename, ios::in|ios::binary)
      

  4.   

    忘了说了,我是加了ios::binary的
    就是ofstream.write((char*),sizeof())
    ifstream.read((char*),sizeof())
      

  5.   

    这是写模块的代码
    bool CGAShortestWay::do_GetGpInfo(int nGp,string& strResult)
    {
        if(!pMember->bEnableInfo)//如果不允许写信息
    return false;
        string msg;
        static char strTemp[50];
    char* temp=strTemp;
         //打开信息文件
    string fn=GetOutName();
    const char* filename=fn.c_str();
    ifstream info(filename,ios::in|ios::binary);
    if(!info)        //如果打开失败
     return false;
    INDIVISEQ popsize;//得到种群规模
    info.read((char*)&popsize,sizeof(popsize));
    sprintf(temp,"种群的规模为%d\r\n",popsize);
    msg+=temp;
    int curGp;
    string tempMsg;
    do//提取出一代的情况
    {
        info.read((char*)&curGp,sizeof(curGp));//得到当前是多少代
    sprintf(temp,"第%d代\r\n",curGp);
    tempMsg+=temp;
    for(int i=0;i<popsize;++i)
    {
    //提取一个个体
    CIndividual in;
    in.clear();
    GENE gene;
    do
    {
    info.read((char*)&gene,sizeof(gene));
                 in.AddGene(gene);
    }while(gene!=0);
    sprintf(temp,"个体%d: ",i);
    tempMsg+=temp;
    //转换为文本形式
    for(int j=0;j<in.GetGenes();++j)
    {
    sprintf(temp," %d ",in.GetGene(j));
    tempMsg+=temp;
    }
    FITNESS fitness;
                info.read((char*)&fitness,sizeof(fitness));
    sprintf(temp," 适应度%f\r\n",fitness);
    tempMsg+=temp;
    }
    if(curGp==nGp)//如果是要查找的
    {
    msg+=tempMsg;
    strResult=msg;
    return true;
    }
    tempMsg="";//清空
    }while(!info.eof());
        return false;
    }
    这是写模块的代码
    //记录状态信息
    inline void CGAShortestWay::WriteInfo(INFO info)
    {
        const string fn=this->GetOutName();//得到信息文件名
    const char* filename=fn.c_str();
                                       //种群应该成熟
    assert(info.pop.IsMature());
        ofstream outf;
        if(info.gn==1)                     //如果是写第一代
    {
    outf.open(filename,ios::out|ios::binary);
    outf.clear();
    if(!outf.good())
    return;
    outf.write((char*)&pMember->para.popsize,
           sizeof(pMember->para.popsize));//文件开头写入种群规模
    }
    else
        outf.open(filename,ios::out|ios::app);//如果文件存在则添加到
        if(!outf.good())                      //末尾。
    return ;        
    outf.seekp(0,ios::end);
        //将这一代的信息写到文件中
        outf.write((char*)&info.gn,sizeof(info.gn));//记录这是多少代
        int popindivis=info.pop.GetIndivis();
        for(int i=0;i<popindivis;++i)
    {//保存每个个体
    CIndividual savIndivi;
    if(!info.pop.GetIndivi(i,savIndivi))
    return;
    int indivis=savIndivi.GetGenes();
    GENE gene;
    for(int j=0;j<indivis;++j)
    {
    gene=savIndivi.GetGene(j);
    outf.write((char*)&gene,sizeof(gene));
    }
    gene=0;//表示个体输入完毕
    outf.write((char*)&gene,sizeof(gene));
    FITNESS fit=savIndivi.GetFitness();
    outf.write((char*)&fit,sizeof(fit));//填入该个体的适应度
    }

    }