Home > Software engineering >  Questions about MFC file reading and writing
Questions about MFC file reading and writing

Time:10-29

/* */save the simulation data 
Void CIntelligentVehicleView: : OnSavetrace ()
{
CFileDialog fileDlg (FALSE);
FileDlg. M_ofn. LpstrFilter="Text Files (*.txt) \ 0 *. TXT \ 0 all Files (*. *) \ 0 *. * \ \ 0 0";
If (IDOK==fileDlg. DoModal ())
{
//CFile file (fileDlg GetPathName (), CFile: : modeWrite);
While (iter!=g_listPath. End ())
{

CNode curpos=* iter.
CFile file;
Cstrings STR.
Cstrings strFilePath;
StrFilePath=fileDlg. GetPathName ();
//cstrings strFileName="mytest. TXT".

SYSTEMTIME st.
GetLocalTime (& amp; St);

STR. The Format (" % 2 d: % 2 d: % 2 d % d % d \ r \ n ", st. wHour, st. wMinute, st. wSecond, curpos. X, curpos. Y);
File. The Open (strFilePath, CFile: : modeCreate | CFile: : modeWrite | CFile: : modeNoTruncate);

File. SeekToEnd ();
File. The Write (LPCTSTR STR, STR., GetLength ());
File. The Close ();
+ + iter.

If (iter==g_listPath. End ())
break;
}
}
}



/* * refer to the simulation data/
Void CIntelligentVehicleView: : OnChecktrace ()
{
//TODO: Add your command handler code here
//CFileDialog DLG (TRUE, "", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT," (*.txt) | *. TXT | | ");
//if (DLG) DoModal ()==IDOK)
//{
//cstrings fileName=DLG. GetPathName ();
//}
CFileDialog fileDlg (TRUE);
FileDlg. M_ofn. LpstrFilter="Text Files (*.txt) \ 0 *. TXT \ 0 all Files (*. *) \ 0 *. * \ \ 0 0";
If (IDOK==fileDlg. DoModal ())
{
CStdioFile mfile (fileDlg GetFileName (), CFile: : modeRead);

Iter=g_listPath. The begin ();
G_listPath. The clear ();

Cstrings m_strTrace="";
The CNode curpos;
//cstrings strFileName="mytest. TXT".
Cstrings strnull="";
Cstrings, y="";
Int pos.

//mfile. Open (strFileName CFile: : modeRead);
While (mfile. ReadString (m_strTrace))
{
Pos=m_strTrace. Find (strnull);
Y=m_strTrace. Right (pos - 2);
Sscanf (x, y, "% d % d", & amp; (curpos. X), & amp; (curpos. Y));
G_listPath. Push_back (curpos);
}
Mfile. Close ();
Loadtrace=true;
}
}
 

This is one I wrote about the file to read and write code, but after I save, read, did not save the document, also don't know what wrong, trouble great god help have a look, my level is limited, thank you

CodePudding user response:

//note written length
File. The Write (LPCTSTR STR, STR., GetLength () * sizeof (TCHAR));

CodePudding user response:

Just this question

CodePudding user response:

CFile file (fileDlg GetPathName (), CFile: : modeWrite);

To change to:

CFile file (fileDlg GetPathName (), CFile: : modeWrite | CFile: : modeCreate);

CodePudding user response:

No file, need the Create option

CodePudding user response:

reference 4 floor wuchuncai response:
no file, need to Create options

Now also is not saved in. TXT file this is how to return a responsibility

CodePudding user response:

reference 4 floor wuchuncai response:
no file, need to Create options

 
/* */save the simulation data
Void CIntelligentVehicleView: : OnSavetrace ()
{
CFileDialog fileDlg (FALSE);
FileDlg. M_ofn. LpstrFilter="Text Files (*.txt) \ 0 *. TXT \ 0 all Files (*. *) \ 0 *. * \ \ 0 0";
FileDlg. M_ofn. LpstrDefExt="TXT";
If (IDOK==fileDlg. DoModal ())
{

CFile file (fileDlg GetPathName (), CFile: : modeReadWrite | CFile: : modeCreate | CFile: : modeNoTruncate);
While (iter!=g_listPath. End ())
{
Cstrings STR.
SYSTEMTIME st.
CNode curpos=* iter.
GetLocalTime (& amp; St);

File. The Open (fileDlg. GetPathName (), CFile: : modeReadWrite | CFile: : modeCreate | CFile: : modeNoTruncate);
STR. The Format (" % 2 d: % 2 d: % 2 d % d % d \ r \ n ", st. wHour, st. wMinute, st. wSecond, curpos. X, curpos. Y);
File. SeekToEnd ();
File. The Write (LPCTSTR STR, STR., GetLength ());
File. The Close ();
+ + iter.

If (iter==g_listPath. End ())
break;

}
}
}

The question now is written to a file open nothing inside! deliver

CodePudding user response:

 STR. The Format (" % 2 d: % 2 d: % 2 d % d % d \ r \ n ", st. wHour, st. wMinute, st. wSecond, curpos. X, curpos. Y); 
File. SeekToEnd ();
File. The Write (LPCTSTR STR, STR., GetLength ());

This has a problem, Write not Write cstrings STR, can only Write the content, can be changed to:
 
STR. The Format (" % 2 d: % 2 d: % 2 d % d % d \ r \ n ", st. wHour, st. wMinute, st. wSecond, curpos. X, curpos. Y);
File. SeekToEnd ();

TCHAR TempStr [4096].
Memset (TempStr, 0409 6 * sizeof (TCHAR));
Lstrcpy (TempStr, STR);

File. The Write (TempStr, lstrlen (TempStr) * sizeof (TCHAR));

CodePudding user response:

 STR. The Format (" % 2 d: % 2 d: % 2 d % d % d \ r \ n ", st. wHour, st. wMinute, st. wSecond, curpos. X, curpos. Y); 
File. SeekToEnd ();
File. The Write (LPCTSTR STR, STR., GetLength ());
File. The Close ();


To:
 STR. The Format (" % 2 d: % 2 d: % 2 d % d % d \ r \ n ", st. wHour, st. wMinute, st. wSecond, curpos. X, curpos. Y); 
TCHAR * pBuffer=STR. GetBuffer (256);
File. SeekToEnd ();
nullnullnull
  • Related