GetOpenFileName( )

Im using GetOpenFileName() to pull in one or multiple files from a user.  The problem is how do I go about parsing the "strFile" field in the OPENFILENAME structure, since it contains many embedded strings?

For example if the user choose "hello.txt and "goodbye.txt" in the "c:\words" folder the string would look like this...

"c:\words\hello.txt"goodbye.txt""

if the user just chose "hello.txt" in the "c:\words" folder the string would look like this...

"c:\words\hello.txt"

Whats a good algrorithm to use to end up with this...

MyString1 = "c:\words\hello.txt"
MyString2 = "c:\words\goodbye.txt"


Thanks,
-Dan
dmaroffAsked:
Who is Participating?
 
NickRepinConnect With a Mentor Commented:
<<If multiple files were selected, the string is of this form:


   Drive: \Directory Name\0FileName 1\0FileName 2\0FileName n\0\0.


The Directory Name is listed first. Then each file that was selected is listed with a terminating NULL Character, except for the last filename, which is terminated with two NULL characters. The two NULL characters signal the end of the string.
If a single file was selected, the string is of this form:


   Drive: \Directory Name\FileName\0\0.


The Directory Name in this case is not terminated by a NULL character, and the file name is terminated with two NULL characters.

Applications must parse the string returned in the lsptrFile member. In doing so, they should make provisions in the parsing code to have a case where the user can make a single selection (even though the OFN_ALLOWMULTISELECT flag is set) or multiple selections.

NOTE: When using OFN_ALLOWMULTISELECT under Windows 95, you need to use the OFN_EXPLORER flag to get the explorer style dialog and NULL terminated strings. If you don't use OFN_EXPLORER with OFN_ALLOWMULTISELECT, you get the old style dialog and space-delimited strings. >>

So,

1) Use OFN_EXPLORER along with OFN_ALLOWMULTISELECT

2)
OPENFILENAME ofn;
GetOpenFileName(...)


//Detect if a single file was selected:
int len=strlen(ofn.lpstrFile);
if(ofn.lpstrFile[len+1]==0) {
   // Single file selected,
   // ofn.lpstrFile contains the full path and file name.
   printf("Single file=%s",ofn.lpstrFile);
}
else {

// ofn.lpstrFile contains directory
printf("Directory=%s",ofn.lpstrFile);

ofn.lpstrFile+=len+1;
// Enumerate all file names.
while(ofn.lpstrFile[0]) {
   // ofn.lpstrFile contains the file name
   printf("Next file=%s",ofn.lpstrFile);
   // Find next name
   len=strlen(ofn.lpstrFile);
   ofn.lpstrFile+=len+1;

}


0
 
dmaroffAuthor Commented:
Ok, Im going to try it out and let you know.

Thanks,
-Dan
0
 
dmaroffAuthor Commented:
Your code worked beautifully.

Thanks a lot,

-Dan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.