These two days I have been engaged in file creation, opening, storage, and some simple read/write operations. Although simple, it is a little difficult for me to be a newbie. Finally, I finally got it done, here, even if others may not see it, it is also good for their own growth.
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////
// Function: Create a New notepad and add a blank record to the notepad file.
// DATA: 6/9/2009
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////
CFileDialog fileDlg (TRUE, _ T ("jsp"), _ T ("*. jsp "),
OFN_OVERWRITEPROMPT, _ T ("Create NewDocment (*. jsp) | *. jsp |"), NULL );
CString PathName;
If (fileDlg. DoModal () = IDOK)
{
PathName = fileDlg. GetPathName ();
CFile file;
CFileException err;
If (file. Open (pathname, cfile: modereadwrite | cfile: modecreate | cfile: modenotruncate, & ERR ))
{
// The New notepad file is successfully created and a record is automatically added.
Cstring m_strrecordcon;
Wchar_t * endptr;
Char struser [1000];
Int nlength = 0;
Wcstombs (struser, m_strrecordcon, 1000 );
Nlength = min (strlen (struser), 1000 );
File. Write (struser, nlength );
If (nlength = 0)
{
Afxmessagebox (L "write record failed! Click Create record and try again! ");
}
Memset (struser, 0, sizeof (struser ));
Updatedata (false );
}
Else
{
Afxmessagebox (_ T ("file creation failed! "));
Err. reporterror ();
Err. Delete ();
}
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////
// Function: Open the notepad File
// DATA: 6/9/2009
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////
CFileDialog dlg (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
L "Data Documents (*. jsp) | *. jsp |", AfxGetMainWnd ());
CString strTem;
If (dlg. DoModal () = IDOK)
{
StrTem = dlg. GetPathName (); // obtain the path name
FileName = dlg. GetFileName (); // get the file name
}
CFile file;
// CString FileName = strTem;
AfxMessageBox (FileName );
Char buf [1000];
Memset (buf,); // initializes the memory to prevent garbled characters at the end of the read.
If (! File. Open (strTem, CFile: modeReadWrite ))
{
// MessageBox (L "no file selected! ");
Return;
}
File. Read (buf, sizeof (buf ));
File. Close ();
M_strRecordCon = buf;
// If the number of file bytes exceeds 400, a prompt dialog box is displayed.
If (m_strRecordCon.GetLength ()> 400)
{
AfxMessageBox (L "this file has more than 400 bytes. Please select another file! ");
}
Memset (buf, 0, sizeof (buf ));
UpdateData (FALSE); // refresh the data in the notepad to the record editing box.
//////////////////////////////////////// //////////////////////////////////////// //////
// Function: Save the file
// DATA: 6/9/2009
//////////////////////////////////////// //////////////////////////////////////// /////
CFileDialog fileDlg (FALSE, _ T ("jsp"), _ T ("*. jsp "),
OFN_OVERWRITEPROMPT, _ T ("Data Documents (*. jsp) | *. * |"), this, 0 );
CString PathName;
If (fileDlg. DoModal () = IDOK)
{
PathName = fileDlg. GetPathName ();
CFile file;
File. Open (PathName, CFile: modeCreate | CFile: modeReadWrite );
// Save user input data
CString struser;
Struser = "";
Wchar_t * endptr;
CString strTem;
Char * writesz;
Writesz = new char [struser. GetLength () + 2];
Wcstombs (writesz, struser, struser. GetLength () + 2 );
File. Write (writesz, strlen (writesz ));
File. Close ();
AfxMessageBox (L "data is saved! /R/n data is stored in: "+ PathName );
Delete writesz;
}
Now we are working on another function for adding records. After completing this function, we will sort out the code and send it to you.