This article describes the VC + + operation of the text file method, the implementation of TXT file at the specified location inserted content. For VC + + enthusiasts have a certain learning reference value.
The main functional code is as follows:
void Cgotofiledlg::onpaint () {if (Isiconic ()) {CPAINTDC DC (this); SendMessage (Wm_iconerasebkgnd, (WPARAM) DC.
GETSAFEHDC (), 0);
int cxicon = GetSystemMetrics (Sm_cxicon);
int cyicon = GetSystemMetrics (Sm_cyicon);
CRect rect;
GetClientRect (&rect); int x = (rect.
Width ()-Cxicon + 1)/2; int y = (rect.
Height ()-Cyicon + 1)/2; dc.
DrawIcon (x, y, M_hicon);
else {cdialog::onpaint (); } hcursor Cgotofiledlg::onquerydragicon () {return (hcursor) M_hicon;} void Cgotofiledlg::onbutopen () {CFileDialog D LG (true,null,null,ofn_hidereadonly| Ofn_overwriteprompt, "All Files (*. TXT) |*. txt| | ", AfxGetMainWnd ());//The construct file opens the dialog box if (DLG). DoModal () = = IDOK)//Decide whether to press the "open" button {M_path = dlg. GetPathName ();
Get file path Files *pfile = fopen (M_path, "r+t");//read-write to open file if (pFile)/To determine whether the file is properly open {char pchdata[1000] = {0};//define data buffer Fread (Pchdata,sizeof (char), 1000,pfile);
Read data to Buffer fclose (pFile);//close File M_file = Pchdata;
} updatedata (FALSE);
} void Cgotofiledlg::onbutinsert () {UpdateData (); File *pfile = fopen (M_path, "r+t");//read-write to open files if (pFile)/To determine whether the file is correctly opened {fseek (pfile,m_goto,seek_set);//Locate File CString St r = M_text + m_file.right (m_file.getlength ()-m_goto);//Set string fputs (str. GetBuffer (0), pFile); Write data to File fseek (pfile,0,seek_set);/relocate file char pchdata[1000] = {0};//define data buffer Fread (pchdata,sizeof (char), 1000,pfile );
Read data to Buffer fclose (pFile);//close File M_file = Pchdata;
UpdateData (FALSE);
}
}
The
Code has a more detailed comment, which is relatively easy to understand. Readers can also further refine the program code to achieve more powerful functionality.