Nick Spot
Nick Spot

Reputation: 267

C getopenfilename getFileName

I have the following code which the problem is, when i'm printing the full path name of the file, i get double spaces between each character in the array.

// initialization outside any class in .c code
OPENFILENAME ofn;       // common dialog box structure
char szFile[260];       // buffer for file name
HWND hwnd;              // owner window
HANDLE hf;              // file handle
...
...
// inside a function
initializeOpenFile();
GetOpenFileName(&ofn);


            for(i = 0; i < sizeof(szFile)/sizeof(char);i++){
                fprintf(stderr,"%c", szFile[i]);
                }
        }
}

void initializeOpenFile(){
    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = TEXT("All\0*.*\0Text\0*.TXT\0");
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
}

Printing me:

enter image description here

I want to use that char array to be passed in to a openFile function:

FILE* fp = fopen( filename, "r" );

Upvotes: 0

Views: 693

Answers (1)

luser droog
luser droog

Reputation: 19504

It looks like it's a wide-string, ie. a string of wide-chars. (Ref: wikipedia)

So szFile should be declared:

wchar_t szFile[260];

Then you can convert it (I think!) with wcstombs().

char szPath[260];
wcstombs(szPath, szFile, 260);

szPath should now contain a "normal" (narrow) character string.

Upvotes: 1

Related Questions