Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File paths 1

Status
Not open for further replies.

darioars

Technical User
Oct 30, 2002
21
0
0
DE
Here I am again!

I have a C++ ATL application where I use a file path in one instruction. Now I have a constant string to define this path (it is, something like "Hello, world!", for instance), but I'd rather have a menu in my window that would allow the user to explore through the computer store devices and load the desired file path.

Do any of you know how to do it?

Thanks a lot in advance, guys!

Darío.
 
You need to try GetOpenFilename or GetSaveFilename

Here is some code

Code:
	char strFile[MAX_PATH] = "";

	OPENFILENAME ofn;

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = GetParent(EditBox);
	ofn.lpstrFilter = "C Files (*.c)\0*.c\0";
	ofn.lpstrFile = strFile;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT;
	ofn.lpstrDefExt = "c";

	if(GetSaveFileName(&ofn))
	{
		SetWindowText(EditBox, strFile);
	}

EditBox is a HWND to an edit box i want to output the data to.

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
if you use MFC you could use CFileDialog like:

CFileDialog x;

switch(x.doModal())
{
case IDOK:
yourFilePath = g.GetPathName()
break;

Ion Filipski
1c.bmp
 
Thank you, Ion!

Do you know how it would be done if I use ATL?
 
Below is an example (on opening a file) that I've extracted from the help itself, but when i run the program, it prompts an error <Run-Time Check Failure #3 - The variable 'hwnd' is being used without being defined.>, but i thought we've defined it in line 3? Can someone pls explain the error?Tanks.


OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = &quot;All\0*.*\0Text\0*.TXT\0&quot;;
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);
}
 
I believe you should try. If you use VisualStudio .NET you can add MFC support to your ATL project.

Ion Filipski
1c.bmp
 
>> The variable 'hwnd' is being used without being defined.

You've created hwnd as a local variable, hwnd should actually be the global hwnd of your application's main window.

if you are using MFC then it is a member variable of your main window called m_hWnd.

If you arent using MFC then you need to store your HWND from CreateWindow in a global variable

#include <windows.h>

HWND g_hWnd = NULL;

int WINAPI WinMain(...)
{
...

g_hWnd = CreateWindow(...);

...
}


Hope that helps.

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
I am having a problem with this function as well

I used this code in my dialog callback to get a filename

case IDC_BUTTON1:
OPENFILENAME ofn;
char fname[300] = &quot;&quot;;
ZeroMemory(ofn, sizeof(ofn));
ofn.nMaxFile = sizeof(ofn);
ofn.lptstrFile = fname;
ofn.lptstrFilter = &quot;bitmap files\0*.bmp\0&quot;;
ofb.lStructSize = sizeof(OPENFILENAME);
ofn.nFilterIndex = 1;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
if(GetOpenFileName(&ofn))
{
//use fname
}
break;

After I use this to get a file, my program is unable to use
direct3d or open files. It crashes when I try to render with
D3D, and fails to open files(without crashing). This situation does not occur when I cancel out of the file selection dialog. It seems as though I am writing to memory that I should not be. The program does succesfully get the filename, but those problems I described continue until termination. Any thoughts?
 
try setting the HWND of the owner:

ofn.hwndOwner = hwnd;

that &quot;may&quot; help, but your code looks ok other than that, are you sure its that section which messes it up? You sure its not when you are actually trying to open the bitmap file or something?

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
I have tried setting the owner to the handle of the application and the dialog from which it is called. I am not using any of the filenames in this part of the program, I am using them to generate a configuration file for another part of the program. Once I have gotten a string from the openfile dialog, fstreams(don't work), GDI(crash), and D3D(crash). I am sure there are countless other things that I haven't used that will also fail. This is why I think that it is writing to some memory that it should not. I am using Visual Studio .NET with Windows XP.
 
I have further traced this problem to nomemory.cpp where it throws an out of memory exception.
 
what causes your out of memory exception?

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
The memory issue appears to have gone away, and I am unable to find what caused it in the first place. I just now tested it with the code I posted a few days ago, and It no longer crashes. However, the program is still unable to use functions from the fstream library. Oddly though, the program executes as though it is writing to the file, this is likely because the buffer is created properly. When it tries to read from the file the filestream returns an error.

string temp;
fstream fin(&quot;file.in&quot;);
getline(fin, temp);
if(!fin)//fin is false after a call to GetOpenFileName,
and true before

There is also an error in the code I posted on Dec 2, the line:
ofn.nMaxFile = sizeof(ofn);

Should have been:
ofn.nMaxFile = 300;

This was not the source the out of memory exception.

**Note**
I found this to be the minimum amount of code necessary to use the Open File Dialog:

OPENFILENAME ofn;
char fileName[MAX_PATH];
fileName[0] = 0;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
if(GetOpenFileName(&ofn))
{
//use fileName
}
 
Alright! Problem solved. For some reason it is necessary to set the flag OFN_NOCHANGEDIR to prevent GetOpenFileName() from
interfering with the fstreams. In frustration, I decided there was a finite number of settings for the OPENFILENAME members, and proceeded to systematically try them all. Luckily, Flags was the thrid member I tried messing with. Setting the flags to 0xffff prevented the dialog from coming up. I then tried 0x00ff, same thing. Finally, with little hope, I tried, 0x000f, and almost magically, it worked. I then went to the commdlg.h file, where these were defined, and tried the four flags individually. This page provides a full description of all the flags for the OPENFILENAME structure. It states that this flag prevents changes to the OS's current working directory(specifically to the directory active in the dialog). I concluded from this that the working directory must not be altered or fstreams will open from the wrong location. However, this does not seem right either because I was testing by running getopenfilename(), and then immediately opening an fstream.
By default, this dialog opens to the current working directory, and I would usually just select a random file without changing directories. At any rate, If you've searched for OPENFILENAME and fstream together, you probably had the same problem as me. Now you can spend five-six hours doing something more productive than tinkering with a f***in' OPENFILEDIALOG, such as studiyng for your exams.
 
haha well done :) glad you got the problem solved!

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top