GetOpenFileName()

So I'm using GetOpenFileName(), and it works. No issues there, found a tutorial and it works great. I wanted to set it up so that if you canceled or didn't select a file, it would keep you in the Open Dialog. Essentially forcing you to select a file. I came up with the following loop. It works, but I have a feeling there is a much better way to accomplish what I want. Any suggestions?
1
2
3
4
5
bool fileFound = 0;
for (fileFound = 0; fileFound == 0; ""){
     GetOpenFileName(&ofn);
     fileFound=GetOpenFileName(&ofn);
     }


Like I said, this works, but something about it doesn't seem right.

Chris
In programming, true or false, means non-zero or zero. The ! operator, is the logical not operator. The logical not operator turns a true into a false, or a false into a true. So you can say while (1), infinite loop, or while (!1), which == while (0) == while (false), no loop.

GetOpenFileName() returns a non-zero or a zero value (Boolean), depending on if it opens a file or not. So, you can just say this.

while ( ! GetOpenFileName(&ofn) ) {}

If GetOpenFileName() returns a 1, meaning that it opened a file, then your terminating condition for the while loop, is now !1, while (! 1), which translates to while (0), or while (false), and it will terminate. Otherwise, if it returns 0, then the not turns it into a 1, or true.

If in another situation you needed to perform the assignment of a value returned from a function at the same time, you could do that like this.

while ( ! (fileFound = GetFileName(&ofn)) ) {}

This way, the assignment is done first, then fileFound is evaluated as a true or false, or something to that effect.

At least this is my understanding.
Last edited on
iseeplusplus wrote:
In C and C++, non-zero or zero mean true or false


* fixed
I'm aware that the bool values of 1 and 0 are True and False. I actually tried it with a while loop. The problem with that approach was if the user cancelled or escaped the file open operation, it would loop, but you would have to select the file twice. Not sure if it's because of the order in which things are evaluated between for loops and while loops.
you would have to select the file twice.


You have to select the file twice because you're calling GetOpenFileName twice. Once on line 3 and again on line 4. Each call to that function opens the File selection dialog.

"you would have to select the file twice."


You have to select the file twice because you're calling GetOpenFileName twice. Once on line 3 and again on line 4. Each call to that function opens the File selection dialog.


I meant when I was using a while loop.

Here's the loop I was using. If I canceled or escaped, it would keep me in the Open Dialog box, which is what I want. Then when I finally did select a file and clicked open, it would open another Open Dialog box and I would have to select the file and click open again.
1
2
3
while (!GetOpenFileName(&ofn)){
		GetOpenFileName(&ofn);		
	}



EDIT: question

Are you saying that this statement pops the dialog open:
while (!GetOpenFileName(&ofn))

Then when I'm in the loop. this obviously pops the dialog open:
GetOpenFileName(&ofn);

Then this statement comes back around to check if the condition is true or false, and once again pops the dialog open:
while (!GetOpenFileName(&ofn))

So basically any time GetOpenFileName(&ofn) is called, even if it's to assign its return value to a bool variable, it's going to pop the dialog box open?

EDIT: answered my question
Yup anytime GetOpenFileName(&ofn); is called, even just for assigning a value to a variable, it pops Open File dialog up.

So after some testing, this works flawlessly:
1
2
3
4
fileFound = 0;
while (fileFound == 0){		
     fileFound = GetOpenFileName(&ofn);
}

Last edited on
Glad you figured it out.

Although as iseeplusplus suggested, you could remove the 'fileFound' variable by putting the GetOpenFileName call in the condition directly:

1
2
3
4
while( !GetOpenFileName(&ofn) )
{
  // do nothing here
}
Very true.

@iseeplusplus:
Didn't mean to doubt you. I just didn't understand what was going on. Now I do and I thank you and Disch.
Also,

1
2
3
while (fileFound == 0){		
     fileFound = GetOpenFileName(&ofn);
}


is equivalent to this,

1
2
3
while (!fileFound){		
     fileFound = GetOpenFileName(&ofn);
}


is equivalent to this,

 
while (! (fileFound = GetOpenFileName(&ofn))){}


The extra parentheses are critical because of order of operations; so that you not after the function call and the assignment.
Last edited on
Topic archived. No new replies allowed.