Hi. I am working on a little program that will be able to open files and read them. The problem is that i CANNOT tell the user to manually type the directory and the name of the file ! It would be madness. So i need to use GetOpenFileName dialog...
Now, i have a little problem. This is the code, i used the example from MSDN, my skills in C++ are quite low so i dont really understand the code...
#include <windows.h>
#include <fstream>
int main()
{
printf("declaring...\n");
// Declare
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // 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);
printf("\n");
printf("opened. exiting...\n");
printf("...");
return 0;
}
Now, my question is HOW CAN I GET the path and the filename ... lets say i want to printf the path and the filename that i selected with this GetOpenFileName function...
MSDN sais "If the user specifies a file name and clicks the OK button, the return value is nonzero. The buffer pointed to by the lpstrFile member of the OPENFILENAME structure contains the full path and file name specified by the user." ... But how can i get them?
Please help, i am sure this is a simple thing for a C++ expert...
PS: This was compiled with VS.Net. I also tried to compile it in DEV C++, but i get an compile error. I suppose it doesnt work with MS Comdlg32.lib...