CString b;
b.Format(_T("TCE_V11"));   

FILE *a;
CString c;
c.Format(_T("E:\\a.txt"));

a = _tfopen(c, _T("w+"));
fprintf(a, "%s", b);
fclose(a); 因为在VS2008中  需要_T()   但是写入文件中只有T
我改为char b[8]="TCE_V13";  这样我知道是可以的
还请各位大侠们指点一下   为什么只输进去了T   要怎么改呢  
而不是像我那样直接就用字符数组了

解决方案 »

  1.   

    既然都用了MFC了  那就没必要直接用API了,直接用CFile好了,下面代码应该没什么问题
    CFile file;
    CString ctx;
    ctx.Format(_T("TCE_V11"));

    file.Open(_T("./test.txt"),CFile::modeCreate|CFile::modeWrite);
    file.SeekToBegin();
    file.Write(ctx,ctx.GetLength()*2);
    file.Flush();
    file.Close();
      

  2.   

    你工程应该是 Unicode 的吧,_T( ) 包住的字符串就成宽字节了, char b[8] 是ANSI的
      

  3.   

    恩,我的工程是Unicode 的
    但是我想问  为什么就只输出一个  T   呢??
      

  4.   

    二进制文件和文本文件的问题,多典型啊...
    你知道这些字符都以ASCII码形式存在内存中,所以会显示到你不如意。
    你尝试这样分开
    'T' 'C' 'E'....‘1’ ‘1’懂我意思么?不懂 搜 二进制文件和文本文件喔
      

  5.   

    比如你把10存进去  出来的时候就是个换行 因为换行的ascii码就是10喽
      

  6.   

    我想说楼主用什么API了,那不就是普通的string类么? 
      

  7.   

    分开?  你说的那些我都懂啊   关键是Unicode  
      

  8.   

    fprintf(a, "%s", b); 改为fprintf(a, "%ws", b); 
    宽字节
      

  9.   

    其实你直接将CString b;改为一个char的也行
      

  10.   

    给你看一下我之前做的文件打开保存文件
    void CdatabaseView::OnFileOpen()
    {
    CFileDialog filedlg(TRUE);
    filedlg.m_ofn.lpstrTitle="请打开您所需的文件";
    //filedlg.m_ofn.lpstrDefExt="txt";//设定默认的打开的文件的类型
    //filedlg.m_ofn.lpstrFilter="text file(*.txt)\0*.txt\0All files(*.*)\0*.*\0\0";
    if(int(filedlg.DoModal())==IDOK)
    {
    //CFile file(filedlg.GetPathName(),CFile::modeCreate|CFile::modeWrite);
    //file.Write("http://www.baidu.com",strlen("http://www.baidu.com"));
    //file.Close(); CString csrtpath=filedlg.GetPathName();
    string strpath=csrtpath.GetBuffer();
    char pat[20];
    strcpy(pat,strpath.c_str()); ifstream infile(pat,ios::in); if(!infile)
    {
    MessageBox("对不起,文件没找到!!!");
    exit(1);
    }
    string school,dept,major,dormy,name,home, sex,pay,no,age;
    for(0;strmaxnumber<5;strmaxnumber++)
    {
    infile>>school>>dept>>major>>dormy>>pay>>name>>sex>>no>>age>>home;
    outedit[strmaxnumber][0].Format("%s",school.c_str());
    outedit[strmaxnumber][1].Format("%s",dept.c_str());
    outedit[strmaxnumber][2].Format("%s",major.c_str());
    outedit[strmaxnumber][3].Format("%s",dormy.c_str());
    outedit[strmaxnumber][4].Format("%s",pay.c_str());
    outedit[strmaxnumber][5].Format("%s",name.c_str());
    outedit[strmaxnumber][6].Format("%s",sex.c_str());
    outedit[strmaxnumber][7].Format("%s",no.c_str());
    outedit[strmaxnumber][8].Format("%s",age.c_str());
    outedit[strmaxnumber][9].Format("%s",home.c_str());
    } Invalidate();
    }
    }void CdatabaseView::OnFileSave()
    {
    CFileDialog filedlg(FALSE);
    filedlg.m_ofn.lpstrTitle="另存为";
    //filedlg.m_ofn.lpstrDefExt="txt";//设定默认的保存的文件的类型
    //filedlg.m_ofn.lpstrFilter="text file(*.txt)\0*.txt\0All files(*.*)\0*.*\0\0";
    if(int(filedlg.DoModal())==IDOK)
    {
    CString csrtpath=filedlg.GetPathName();
    string strpath=csrtpath.GetBuffer();
    char pat[20];
    strcpy(pat,strpath.c_str());
    ofstream outfile(pat,ios::out);
    if(!outfile)
    {
    exit(1);
    }
    /*outfile<<"学院 "<<"  系别  "<<" 专业 "<<"  宿舍 "<<"缴费 ";
    outfile<<"  姓名 "<<" 性别 "<<" 年龄 "<<" 学号 "<<"  籍贯 "<<endl;
    string str="    ";
    for(int m=0;m<strmaxnumber;m++)
    {
    outfile<<outedit[m][0]<<str<<outedit[m][1]<<str<<outedit[m][2]<<str
    <<outedit[m][3]<<str<<outedit[m][4]<<str<<outedit[m][5]<<str
    <<outedit[m][6]<<str<<outedit[m][7]<<str<<outedit[m][8]<<str
    <<outedit[m][9]<<endl;

    }*/
    //下面存储的数据格式更好
    outfile<<"学院 "<<" 系别 "<<" 专业 "<<" 宿舍 "<<" 缴费 "
    <<" 姓名 "<<" 性别 "<<" 年龄 "<<" 学号 "<<" 籍贯 "<<endl;
    for(int m=0;m<strmaxnumber;m++)
    {
    outfile<<outedit[m][0]<<"  "<<outedit[m][1]<<"  "<<outedit[m][2]<<"  "<<outedit[m][3]
    <<"    "<<outedit[m][4]<<"    "<<outedit[m][5]<<"   "<<outedit[m][6]<<"    "<<outedit[m][7]
    <<"    "<<outedit[m][8]<<"   "<<outedit[m][9]<<endl;
    } }
    }
      

  11.   

    给你看一下我之前做的文件打开保存文件
    void CdatabaseView::OnFileOpen()
    {
    CFileDialog filedlg(TRUE);
    filedlg.m_ofn.lpstrTitle="请打开您所需的文件";
    //filedlg.m_ofn.lpstrDefExt="txt";//设定默认的打开的文件的类型
    //filedlg.m_ofn.lpstrFilter="text file(*.txt)\0*.txt\0All files(*.*)\0*.*\0\0";
    if(int(filedlg.DoModal())==IDOK)
    {
    //CFile file(filedlg.GetPathName(),CFile::modeCreate|CFile::modeWrite);
    //file.Write("http://www.baidu.com",strlen("http://www.baidu.com"));
    //file.Close(); CString csrtpath=filedlg.GetPathName();
    string strpath=csrtpath.GetBuffer();
    char pat[20];
    strcpy(pat,strpath.c_str()); ifstream infile(pat,ios::in); if(!infile)
    {
    MessageBox("对不起,文件没找到!!!");
    exit(1);
    }
    string school,dept,major,dormy,name,home, sex,pay,no,age;
    for(0;strmaxnumber<5;strmaxnumber++)
    {
    infile>>school>>dept>>major>>dormy>>pay>>name>>sex>>no>>age>>home;
    outedit[strmaxnumber][0].Format("%s",school.c_str());
    outedit[strmaxnumber][1].Format("%s",dept.c_str());
    outedit[strmaxnumber][2].Format("%s",major.c_str());
    outedit[strmaxnumber][3].Format("%s",dormy.c_str());
    outedit[strmaxnumber][4].Format("%s",pay.c_str());
    outedit[strmaxnumber][5].Format("%s",name.c_str());
    outedit[strmaxnumber][6].Format("%s",sex.c_str());
    outedit[strmaxnumber][7].Format("%s",no.c_str());
    outedit[strmaxnumber][8].Format("%s",age.c_str());
    outedit[strmaxnumber][9].Format("%s",home.c_str());
    } Invalidate();
    }
    }void CdatabaseView::OnFileSave()
    {
    CFileDialog filedlg(FALSE);
    filedlg.m_ofn.lpstrTitle="另存为";
    //filedlg.m_ofn.lpstrDefExt="txt";//设定默认的保存的文件的类型
    //filedlg.m_ofn.lpstrFilter="text file(*.txt)\0*.txt\0All files(*.*)\0*.*\0\0";
    if(int(filedlg.DoModal())==IDOK)
    {
    CString csrtpath=filedlg.GetPathName();
    string strpath=csrtpath.GetBuffer();
    char pat[20];
    strcpy(pat,strpath.c_str());
    ofstream outfile(pat,ios::out);
    if(!outfile)
    {
    exit(1);
    }
    /*outfile<<"学院 "<<"  系别  "<<" 专业 "<<"  宿舍 "<<"缴费 ";
    outfile<<"  姓名 "<<" 性别 "<<" 年龄 "<<" 学号 "<<"  籍贯 "<<endl;
    string str="    ";
    for(int m=0;m<strmaxnumber;m++)
    {
    outfile<<outedit[m][0]<<str<<outedit[m][1]<<str<<outedit[m][2]<<str
    <<outedit[m][3]<<str<<outedit[m][4]<<str<<outedit[m][5]<<str
    <<outedit[m][6]<<str<<outedit[m][7]<<str<<outedit[m][8]<<str
    <<outedit[m][9]<<endl;

    }*/
    //下面存储的数据格式更好
    outfile<<"学院 "<<" 系别 "<<" 专业 "<<" 宿舍 "<<" 缴费 "
    <<" 姓名 "<<" 性别 "<<" 年龄 "<<" 学号 "<<" 籍贯 "<<endl;
    for(int m=0;m<strmaxnumber;m++)
    {
    outfile<<outedit[m][0]<<"  "<<outedit[m][1]<<"  "<<outedit[m][2]<<"  "<<outedit[m][3]
    <<"    "<<outedit[m][4]<<"    "<<outedit[m][5]<<"   "<<outedit[m][6]<<"    "<<outedit[m][7]
    <<"    "<<outedit[m][8]<<"   "<<outedit[m][9]<<endl;
    } }
    }