|
-
Jul 18th, 2001, 10:14 PM
#1
Thread Starter
PowerPoster
Returing a string!
I want to write my own function that shows an "OpenFile DialogBox" and when somebody selects a file, I want to return the filename at the end of my function
Here is the code:
VB Code:
void ShowOpenFile(HWND hwndowner)
{
OPENFILENAME ofn;
char chosenfile[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
chosenfile[0] = 0;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST, OFN_HIDEREADONLY;
ofn.hInstance = hinst;
ofn.hwndOwner = hwndowner;
ofn.lpstrFilter = "Web Page(*.html)\0*.html\0\0";
ofn.lpstrDefExt = "html";
ofn.lStructSize = MAX_PATH;
ofn.lpstrFile = chosenfile;
if(GetOpenFileName(&ofn))
{
return chosenfile[MAX_PATH];
}
}
That code does not work and I have tried lots of other things like making a char function instead of void function but it does not solve the problem.
So simply... How do return the name of the file at the end of my function?
-
Jul 19th, 2001, 04:11 AM
#2
Frenzied Member
Well you are currently trying to return a single character in the array of characters that make up your string. Not only that, but since you declared the array to be of size MAX_PATH it will be indexed from 0 to MAX_PATH-1 so you are using a character outside the range of the string.
If you want to return a string you need to return the whole array, not just a single character. You are actually just returning a pointer to the first element in the array when you do this.
In order to return any value, though, you need to change the return type to something other than void, and if you're returning a string (an array of characters) then you will be returning a pointer to a character, of type char *.
Try this:
Code:
char *ShowOpenFile(HWND hwndowner)
{
OPENFILENAME ofn;
char chosenfile[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
chosenfile[0] = 0;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST, OFN_HIDEREADONLY;
ofn.hInstance = hinst;
ofn.hwndOwner = hwndowner;
ofn.lpstrFilter = "Web Page(*.html)\0*.html\0\0";
ofn.lpstrDefExt = "html";
ofn.lStructSize = MAX_PATH;
ofn.lpstrFile = chosenfile;
if(GetOpenFileName(&ofn))
return chosenfile;
else
return (char *)NULL; // in case the GetOpenFileName call fails
}
Harry.
"From one thing, know ten thousand things."
-
Jul 19th, 2001, 05:33 AM
#3
Monday Morning Lunatic
Code:
char chosenfile[MAX_PATH];
Make this static, then you can safely return a pointer. However the value will be overwritten each time you call the function.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 19th, 2001, 06:47 AM
#4
transcendental analytic
chosenfile runs out of scope Harry so here's two things i can think of.
1. you pass the chosenfile pointer
2. you return a pointer to a newed char array.
and in the second alternative you have to delete it when you're done with it.
and of course, there is the parksie-way but dunno if that's how most programmers want it
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 19th, 2001, 09:21 AM
#5
-
Jul 19th, 2001, 06:30 PM
#6
transcendental analytic
I want to believe you Harry, I have to put it that way, since I believe what I know.
I think the pass pointer way is the most reasonable, since you get to choose if it should be on the heap or not, and it certainly isn't that much work. The static var way would be suggesting eventual problems if you don't take into account what the function does when it is called, and that somewhat undermines object orientation (paranoid I know). Also I forgot to mention newing and returning a pointer to the heap would be again, bad programming practice, I guess.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|