-
Using OPENFILENAME
Hey, I am really lacking in windows programming, but I'm trying to use the windows openfilename structure. It is just for a console program, I'm not registering any windows. I just want to have a simple way to load a txt file.
The part where I get thrown is what to do after the file is selected. (I think).
Here is what I have so far:
Code:
char CurrentFileName[1024];
OPENFILENAME OpenFilename;
memset(&OpenFilename, 0, sizeof(OpenFilename));
OpenFilename.lStructSize = sizeof(OpenFilename);
OpenFilename.hwndOwner = 0;
OpenFilename.hInstance = 0;
OpenFilename.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
OpenFilename.lpstrFile = CurrentFileName;
OpenFilename.nMaxFile = sizeof(CurrentFileName);
OpenFilename.Flags = OFN_FILEMUSTEXIST;
OpenFilename.lpstrDefExt = "txt";
if(GetOpenFileName(&OpenFilename)) { //Am I using the right function here?
//What needs to happen here?
}
Thanks,
David
-
-
I took the sample from one of the links above... And you know - it is working.
Code:
#include <windows.h>
int main()
{
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd = NULL; // owner window
HANDLE hf; // file handle
// 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.
//
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;
// 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);
return 0;
}
-
Ok, sorry about the ignorant question, but it appears that all those links explain how to open the save and open dialog boxes. Once you select a file, is it possible to use if and of streams on the chosen file. I guess the filename that you get from GetOpenFileName is a lp, and I don't think you can use that with if/of stream
Thanks for being patient with me,
David
-
>> I guess the filename that you get from GetOpenFileName is a lp, and I don't think you can use that with if/of stream
Nope. In fact, it is usable, and it is in the array 'char szFile[260];'. Simple put, ofn.lpstrFile points to, or is essentially an alias for, szFile, and is written to by GetOpenFileName
-
Awesome! That worked. I had been trying to use the lpstrFile from the structure.....I wonder about my self sometimes.
Thanks, Tonto!
-
Well, just beating a dead horse, with a flail, you should be able to use the lpstrFile from the structure to do it. I'd delight in seeing the compiler errors <salivating>
-
Huh, your right. That also worked, but before I wasn't setting lpstrFile to null. Or maybe I'm getting confused build error when I forgot to include the comdlg32.lib. I don't really know, because I'm sleepy and stupid....stupid-sleepy.
Sorry if I got your hopes up :p
-
One more thing, though. Doing it this way is not unicode-compatible.
Coming up with a good solution has some issues:
-- The standard streams, whether in their wide or narrow variant, take a narrow-character string as the file name. This means that in Unicode builds, you have to convert from UTF-16 to some narrow encoding. (This is an often-complained-about problem with the streams.)
-- The filenames could contain characters that are not representable in any narrow encoding but UTF-8, which would render such files unopenable by your code as unknown characters get replaced by question marks.
-- Windows does not support UTF-8 as a narrow encoding.
All in all, the situation is a mess. There is no good solution, really, except finding some 3rd-party components that provide a stream interface for wide-character file names.
-
Hey, thanks for the information CornedBee. That kinda sucks, but it's not that big a deal because it was just a learning program. An encryptor actaully.