从 choosefile dlg 可以得到一个 OPENFILENAME, 进而可以创建一个 file handle, 但我想要的是一个 FILE * pFILE: 应该如何呢?
<Section 摘自http://blog.csdn.net/dijkstar/archive/2007/11/14/1884105.aspx>
_open_osfhandle()函数在windows下可以将一个win32打开的文件句柄映射成为一个普通的C库里面的流文件接口,参见下例:
</Section>
这样,开启动话框并打开一个文件所需的代码:
<Section 摘自http://blog.csdn.net/dijkstar/archive/2007/11/14/1884105.aspx>
_open_osfhandle()函数在windows下可以将一个win32打开的文件句柄映射成为一个普通的C库里面的流文件接口,参见下例:
- void OSFileToCRTFile()
- {
- HANDLE hFile = CreateFile("c://test.dat", GENERIC_READ | GENERIC_WRITE, 0, NULL,
- OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- char szText[] = "Hello world!/n";
- DWORD dwWritten;
- WriteFile(hFile, szText, strlen(szText), &dwWritten, NULL);
- FILE* pFile = NULL;
- int nHandle = _open_osfhandle((long)hFile, _O_TEXT | _O_APPEND);
- if (nHandle != -1)
- pFile = _fdopen(nHandle, "wt");
- if(pFile)
- {
- int n = fputs("write by FILE*!", pFile);
- fflush(pFile);//立即写入文件
- // n == 0
- }
- CloseHandle(hFile);
- }
这样,开启动话框并打开一个文件所需的代码:
- OPENFILENAME ofn; // common dialog box structure
- char szFile[260] = {0}; // buffer for file name
- //HWND hwnd ; // owner window
- HANDLE hf; // file handle
- printf("initialising.../n");
- // Initialize OPENFILENAME
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- //ofn.hwndOwner = hwnd;
- ofn.lpstrFile = szFile;
- //
- // Set lpstrFile[0] to '/0' so that GetOpenFileName does not
- // use the contents of szFile to initialize itself.
- //
- printf("setting.../n");
- ofn.lpstrFile[0] = '/0';
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFilter = "All/0*.*/0Text/0*.TXT/0";
- ofn.nFilterIndex = 1;
- ofn.lpstrFileTitle = NULL;
- ofn.nMaxFileTitle = 0;
- ofn.lpstrInitialDir = NULL;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
- printf("displaying.../n");
- // Display the Open dialog box.
- if (GetOpenFileName(&ofn)==TRUE) {
- hf = CreateFile(ofn.lpstrFile, GENERIC_READ,0, (LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);
- int nhandle = _open_osfhandle((long)hf, _O_RDONLY | _O_TEXT);
- if (nhandle != -1){
- FILE * pFile = _fdopen(nhandle, "r");
- TestRead * pTest = new TestRead(pFile);
- pTest->readVertices();
- fclose(pFile);
- }
- CloseHandle(hf);
- }