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?
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), orwhile (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.
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 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 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.