質問

I'm trying to create an xml file using pugixml. The code is;

//Open the save as diolog
TCHAR szFilters[]= _T("Files (*.abc)|*.abc|All Files (*.*)|*.*||");

// Create an SaveAs dialog; the default file name extension is ".abc".
CFileDialog fileDlg(FALSE, _T("abc"), NULL,
    OFN_OVERWRITEPROMPT |OFN_CREATEPROMPT| OFN_PATHMUSTEXIST, szFilters);

// Display the file dialog. 
CString pathName;
CString fileName;
if(fileDlg.DoModal() == IDOK)
{
    pathName = fileDlg.GetPathName(); 
    fileName = fileDlg.GetFileName();

    ::CreateFile(pathName,GENERIC_WRITE,0,NULL,CREATE_NEW, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);   
} //File is created in explorer
else
    return;

//[code_modify_add
// add node with some name
pugi::xml_document xmlDoc;
pugi::xml_parse_result result = xmlDoc.load_file(fileName);


The problem is result always gives out a 'file_not_found' status, but I can see that the file is created in windows explorer. When I try to select the same file during the execution of the program it still returns 'file_not_found'.
However if I close the program and run again and then select the file, result returns true.
I noticed that while the program is executing I cannot open the newly created file, but when the program is closed I can open it.
What could be the matter with it?

Thanks.

役に立ちましたか?

解決

You are creating a file and leaving it open write only with a share mode of zero (meaning it can not be shared) and throwing away its handle and then trying to reopen the file for reading with the xml parser.

You probably want to CloseHandle() on the return value for ::CreateFile()

HANDLE hFile = ::CreateFile(pathName,GENERIC_WRITE,0,NULL,CREATE_NEW, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL); 

if (hFile == INVALID_HANDLE_VALUE) {
  // Call GetLastError() to figure out why the file creation failed.
}
else
{
  CloseHandle(hFile);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top