Thread: openfilename with spaces in path

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    28

    openfilename with spaces in path

    How is it possible to make an openfile dialog box, where spaces are allowed in the path? Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends probably on what toolkit you're using to make the dialog box in the first place, I guess. I'm not an expert on the various different kinds, or on any in particular, but most of them don't have trouble with spaces, to the best of my understanding.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    This is the code I am using:

    Code:
    void Opendialog(HWND hwnd){
    	OPENFILENAME ofn;
    	TCHAR buf[60];
    	ZeroMemory(&ofn, sizeof(ofn));
    	ofn.lStructSize=sizeof(ofn);
    	ofn.lpstrFile=buf;
    	ofn.lpstrFile[0]='\0';
    	ofn.hwndOwner=hwnd;
    	ofn.nMaxFile=sizeof(buf);
    	ofn.lpstrFilter=TEXT("All Files(*.*)\0*.*\0");
    	ofn.nFilterIndex=1;
    	ofn.lpstrInitialDir=NULL;
    	ofn.lpstrFileTitle=NULL;
    	ofn.Flags= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
    	if(GetOpenFileName(&ofn))
    		Loadfile(ofn.lpstrFile);
    }
    
    void Loadfile(LPSTR file){
    	HANDLE hfile;
    	DWORD dwsize;
    	DWORD dw;
    	LPBYTE Ipbuffer=NULL;
    
    	hfile=CreateFile(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    	dwsize=GetFileSize(hfile, NULL);
    	Ipbuffer=(LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, dwsize+1);
    	ReadFile(hfile, (LPWSTR)Ipbuffer, dwsize, &dw, NULL);
    	CloseHandle(hfile);
    	Ipbuffer[dwsize]=0;
    	SetWindowText(ghwndedit, (const char *)Ipbuffer);
    	HeapFree(GetProcessHeap(), 0, Ipbuffer);
    }

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    > ofn.lpstrFilter=TEXT("All Files(*.*)\0*.*\0");

    needs a second '\0'

    ofn.lpstrFilter=TEXT("All Files(*.*)\0*.*\0\0");

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    Sorry for coming back after too long, but it didn't work.
    Edit: Anyway, solved. CommDlgExtendedError showed error 12291, which means that lpstrFile buffer is too small. In other words, problem was the inadequate space allocated for the path and not the space characters in it.
    Last edited by heisel; 09-25-2009 at 01:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting file path with spaces
    By Opariti in forum C Programming
    Replies: 16
    Last Post: 08-19-2009, 04:16 AM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  4. MCI Playing MP3s - Spaces in path
    By darkrifter in forum Windows Programming
    Replies: 2
    Last Post: 06-16-2005, 12:49 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM