一步一步排查,一开始在子线程里fopen,后来移动到AfxBeginThread之前,依然失败(指没有创建文件,fopen函数却返回成功)。直接移动到button函数最前,成功。然后顺序往下移动,发现当选择文件时,即使用CFileDialog函数时,再fopen就失败。后来用msdn上的CfileDialog的例子
“// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
char CChildFrame::szFilters[]=
"MyType Files (*.my)|*.my|All Files (*.*)|*.*||";
// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg (TRUE, "my", "*.my",
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
// Display the file dialog. When user clicks OK, fileDlg.DoModal()
// returns IDOK.
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();
// Implement opening and reading file in here.
...
//Change the window's title to the opened file's title.
CString fileName = fileDlg.GetFileTitle ();
"a"
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist.
如果文件不存在,会创建的,"w"方式我试过了,不行的。