你好,请问,MFC中怎样在指定的路径创建一个TXT文档,并把指定数组中的数据写入其中,数组更新一次写一次

2024-11-28 11:54:04
推荐回答(4个)
回答(1):

void C文件读写Dlg::OnWriteButton()
{
char* pszFileName="F:\\myfile.txt";
CStdioFile myFile;
CFileException fileException;
if(myFile.Open(pszFileName,CFile::typeText|CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate),&fileException)
{
CString strOrder;
strOrder.Format("%d,%.3f\t",66,88.88);
myFile.WriteString(strOrder);
myFile.Close(); }
else
{
TRACE("Can't open file %s,error=%u\n",pszFileName,fileException.m_cause);
}
}

回答(2):

void CTestDlgDlg::OnGetpath()
{
// TODO: Add your control notification handler code here
char *pch="abdcefghigklmnop";
CFileDialog dlg(FALSE,"txt","baocun",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"TEXT FILES(*.txt)\0.txt\0");
if(IDOK==dlg.DoModal())
{
CFile fp(dlg.GetFileName(),CFile::modeCreate | CFile::modeWrite |CFile::modeNoTruncate);
fp.SeekToEnd();
fp.Write(pch,strlen(pch));
fp.Close();
}
}
每次再次写的时候会弹出对话框,文件已存在,是否替换,选是就可以了,如果嫌麻烦,
CFileDialog dlg(FALSE,"txt","baocun",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"TEXT FILES(*.txt)\0.txt\0");
把这句改成CFileDialog dlg(FALSE,"","suibian");保存名字也不用取,因为是用文件其实是用CFile创建的,名字由CFile文件指定
吧你要写的数组替换掉我的字符串就可以了

回答(3):

char filename[]="C:\\test.txt";
char message[]="this is a test message\r\n";
FILE* hFileDst = fopen(filename, "wb");
fprintf(hFileDst, message);
fclose(hFileDst);

回答(4):

#include