The most recent project needs to read a file in a particular format. and real-time text file content updates to the edit box to go, now the function has been achieved, special will be sent up the source, hoping to help some people, because the level is not high, may be the optimization of code and quality is not very high, forgive me.
Open a Notepad file that already exists
CFileDialog Dlg (True,null,null, ofn_hidereadonly| Ofn_overwriteprompt| Ofn_allowmultiselect,
L "Data Documents (*.jsp) |*.jsp|", AfxGetMainWnd ());
CString Strtem;
if (dlg. DoModal () ==idok)
{
Strtem= dlg. GetPathName ()//Get the filename, you can use this parameter to manipulate the file, such as fopen ()
MessageBox (Strtem)//Use MessageBox to eject the name of the file you selected.
}
CFile file;
CString Filename=strtem;
Char buf[16000]; Read 16K memory
memset (buf,0,16000);//Initialize the memory to prevent garbled characters at the end of the read character
if (!file. Open (Filename,cfile::moderead))
{
MessageBox (L "No files selected!");
Return
}
File. Read (buf,sizeof (BUF));
File. Close ();
AfxMessageBox (BUF);
m_strrecordcon=buf;//to the text box CString m_data
Sets the limit for a byte number, which is 400 bytes
If the file has a number of bytes greater than 400, the pop-up Reminder dialog box
if (M_strrecordcon.getlength () >400)
{
AfxMessageBox (L) The file has a byte number greater than 400, please select a different file. ");
}
UpdateData (FALSE); Data refresh in Notepad to record edit box display
}