2013-11-23 47 views
0

我有GetOpenFileName的問題。 在調用GetOpenFileName方法WriteData正在工作之前。 寫數據有一個簡單的文件檢查程序:獲取打開的文件名正在查殺ifstream

ifstream inFile(FileName.c_str()); 
if (!inFile.is_open()) return false; 

OFN轉儲: Dump
並調用GetOpenFileName後其始終爲false。但在調用GetOpenFileName之前,它工作正常。

while (!lang->LoadLang(config->getLanguagePath())) 
{ 
    OPENFILENAME ofn = {0}; 
    ofn.lStructSize = sizeof(ofn); 
    ofn.lpstrFile = config->getLanguagePath(); 
    ofn.lpstrFile[0] = '\0'; 
    ofn.lpstrTitle = "Select the language file"; 
    ofn.nMaxFile = 255; 
    ofn.lpstrFilter = "Language file\0*.lng\0"; 
    ofn.nFilterIndex = 1; 
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 
    if (GetOpenFileName(&ofn) == 0) return 0; 
    config->WriteData(KEY_LANG, ofn.lpstrFile); 
} 
+0

您是否嘗試過使用調試器來查看'GetOpenFileName'調用後的結構看起來像什麼? –

+0

檢查出來。問題已更新。 –

+0

這看起來像一個有效的文件名,所以無論出現什麼問題似乎都在你沒有顯示的代碼中。 –

回答

0

問題解決了。我試圖使用WinAPI函數OpenFile打開文件,並得到錯誤代碼2(找不到文件)。 現在我修好了。

char buffer[255]; 
GetCurrentDirectoryA(255, buffer); 
OPENFILENAME ofn = {0}; 
ofn.hInstance = hInstance; 
ofn.lStructSize = sizeof(OPENFILENAME); 
ofn.lpstrFile = config->getLanguagePath(); 
ofn.lpstrFile[0] = '\0'; 
ofn.lpstrTitle = "Select the language file"; 
ofn.nMaxFile = 255; 
ofn.lpstrFilter = "Language file\0*.lng\0"; 
ofn.nFilterIndex = 1; 
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 
if (GetOpenFileName(&ofn) == 0) return 0; 
SetCurrentDirectory(buffer); 
config->WriteData(KEY_LANG, ofn.lpstrFile);