Using OPENFILENAME

This is a discussion on Using OPENFILENAME within the Windows Programming forums, part of the Platform Specific Boards category; Hey, I am really lacking in windows programming, but I'm trying to use the windows openfilename structure. It is just ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    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

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    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

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> 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

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  6. #6
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Awesome! That worked. I had been trying to use the lpstrFile from the structure.....I wonder about my self sometimes.

    Thanks, Tonto!

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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>

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  8. #8
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    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

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save vs Save As.... OPENFILENAME flags
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-01-2008, 02:42 PM
  2. Openfilename
    By Gordon in forum Windows Programming
    Replies: 15
    Last Post: 09-22-2007, 04:37 PM
  3. OPENFILENAME structure
    By SuperNewbie in forum Windows Programming
    Replies: 1
    Last Post: 01-22-2004, 06:51 AM
  4. OPENFILENAME ofn;
    By ROCKaMIC in forum Windows Programming
    Replies: 22
    Last Post: 10-17-2003, 03:48 AM
  5. OPENFILENAME and multiple files.
    By -leech- in forum Windows Programming
    Replies: 4
    Last Post: 02-25-2003, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21