急!如何在mfc中向txt文件写入数据,用CFile!!

2024-11-06 00:26:13
推荐回答(4个)
回答(1):

MFC中不需要加什么东西,直接使用CFile类;
CFile WriteFile;
WriteFile.Open("d:\\file.txt",CFile::modeWrite|CFile::modeRead|CFile::modeCreate);
char *WriteBuf="12345";
WriteFile.Write(WriteBuf,strlen(WriteBuf)+1);
delete WriteBuf;
放在一个Button控件试试吧。

回答(2):

char* pszFileName="F:\\myfile.txt";
CStdioFile myFile;
CFileException fileException;
if(myFile.Open(pszFileName,CFile::typeText|CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate),&fileException)
{
myFile.SeekToEnd();
myFile.WriteString(stime);
myFile.WriteString("\n");
myFile.SeekToEnd();
myFile.WriteString(str1);
myFile.WriteString("\n");
myFile.SeekToEnd();
myFile.WriteString(str2);
myFile.WriteString("\n");
myFile.SeekToEnd();
myFile.WriteString(str3);
myFile.WriteString("\n");
myFile.Close();
MessageBox("文件写入成功","提示");
}
else
{
TRACE("Can't open file %s,error=%u\n",pszFileName,fileException.m_cause);
}

回答(3):

你在MFC调用输出函数的那个CPP文件中加入#include
和using namespace std;就可以直接用你上面那两句话了。记得用fclose(stdout);关闭文件哦。

回答(4):

把配置属性->常规里的字符集改成“使用多字节字符集”试试