mfc打开文件的问题

mfc打开文件的问题,第1张

使用MFC读取文件中的内容是程序设计中经常用到的功能

CFileDialog cFileDialog( TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,

"碰正弯All Files (*.*)|*.*||"笑闷, AfxGetMainWnd())//第1步 取得路径

CString strFilePath = ""

CString strFileName = ""

if(cFileDialog.DoModal()==IDOK)

{

m_strFilePath = cFileDialog.GetPathName()

strFileName = cFileDialog.GetFileName()

UpdateData(false)

}

int input = 0

ifstream inout

inout.open(strFileName,ios::in)

if(!inout)

{

cerr<<"Unable to open file!"<<endl

}

else

{

cout<<"打开文件成功"<<endl

}

char nextline[256]

CString temp_str = ""

CString m_temp = ""

while(inout.getline(nextline, sizeof(nextline)))

{

temp_str = nextline

m_temp += temp_str

m_temp += "\清运r\n"//MFC的编辑框需要使用“\r\n”来识别换行,单单一个“\n”是不够的。

}

m_original = m_temp

AfxMessageBox(m_original)

UpdateData(false)

inout.close()

inout.clear()

//写入文件

void CMFCApplication1Dlg::OnBnClickedWriteBtn()

{

// MFC的CFile类写入文件

CFile file(L"1.txt"耐搜, CFile::modeCreate | CFile::modeWrite )

char *cstr=new char[128]

GetDlgItemTextA(m_hWnd, IDC_EDIT1, cstr,128)

int n = strlen(cstr)

file.Write(cstr,strlen(cstr))

file.Close()

}

//读取文慎巧件

void CMFCApplication1Dlg::OnBnClickedReadfileBtn()

{

/昌孝历/ MFC的CFile类读取文件

CFile file(L"1.txt", CFile::modeRead)

int n=file.GetLength()

char *pStr = new char[n+1]

file.Read(pStr, n)

pStr[n ] = 0

file.Close()

SetDlgItemTextA(m_hWnd, IDC_EDIT1, pStr)

}

可以使用传统的pascal *** 作文件的方法或者使用文件流。

1、传统文团知件 *** 作世好方法

assignfile(f,'c:\test')

rewrite(f)

用blockwrite或write写入二进制信息

closefile(f)

2、文件流

try

f:=tfilestream.create(filename,fmcreate

or

fmopenwrite)

文件 *** 作搜或铅,写入数据

finally

f.free

end


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/tougao/8205295.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存