Results 1 to 6 of 6

Thread: Returing a string!

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    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:
    1. void  ShowOpenFile(HWND hwndowner)
    2. {
    3.     OPENFILENAME ofn;
    4.     char chosenfile[MAX_PATH];
    5.  
    6.     ZeroMemory(&ofn, sizeof(ofn));
    7.     chosenfile[0] = 0;
    8.  
    9.     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST, OFN_HIDEREADONLY;
    10.     ofn.hInstance =     hinst;
    11.     ofn.hwndOwner =     hwndowner;
    12.     ofn.lpstrFilter =   "Web Page(*.html)\0*.html\0\0";
    13.     ofn.lpstrDefExt =   "html";
    14.     ofn.lStructSize = MAX_PATH;
    15.     ofn.lpstrFile = chosenfile;
    16.  
    17.     if(GetOpenFileName(&ofn))
    18.     {
    19.         return chosenfile[MAX_PATH];
    20.     }
    21.    
    22. }

    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?
    Baaaaaaaaah

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You may not believe me but I was well aware of that at the time I wrote my reply

    I was counting on the string being used as soon as it returns but I guess I was just being lazy, sorry

    What I was going to suggest if I hadn't been so lazy was to use a static, like Parksie said. Either that or pass in a string pointer, but the static seems like less work, and that's important when you're this lazy
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width