在MFC中,怎么把CString写入文件???

void CnewoneDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码

EndDialog(1);
}
/*结构体为
typedef struct {
CString Sex;
CString Num;
CString Name;
CString Adress;
CString QQ;
CString Phone;
}Base;
Base A[50];
要求一行一行输出到D:TXL.TXT文件上。最好能讲一下怎么做的
*/


BOOL CnewoneDlg::OutputBase(void)
{
    // 1 组合到总字符串strSum中
    CString strSum = _T(""), strTmp;

    for(int i=0; i<50; i++)
    {
        strTmp.Format(_T("\r\n\r\n%d."), i);

        strSum += strTmp;
        strSum += _T("\r\n Sex:   ") + A[i].Sex;
        strSum += _T("\r\n Num:   ") + A[i].Num;
        strSum += _T("\r\n Name:  ") + A[i].Name;
        strSum += _T("\r\n Adress:") + A[i].Adress;
        strSum += _T("\r\n QQ:    ") + A[i].QQ;
        strSum += _T("\r\n Phone: ") + A[i].Phone;
    }

    // 2 写入文件
    CFile cf;
    if(!cf.Open(_T("D:\\TXL.TXT"), CFile::modeCreate|CFile::modeWrite))
    {
        AfxMessageBox(_T("D:\\TXL.TXT \r\n Open failed when write."));
        return FALSE;
    }

    int len = strSum.GetLength()
    cf.Write(strSum.GetBuffer(len), len);
    cf.Close();

    strSum.ReleaseBuffer();

    return TRUE;
}

void CnewoneDlg::OnBnClickedButton3()
{
// TODO:  在此添加控件通知处理程序代码
OutputBase();
EndDialog(1);
}

没有其他源码, 没有编译, 可能有些小错误, 楼主自己排排

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-01
可以写个配置文件来保存,而且还可以随时调用
相似回答