Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save file with GetSaveFileName in win32?

I write this code to get fileName to save my file :

#include "stdafx.h"
#include <windows.h>


int _tmain(int argc, _TCHAR* argv[])
{            
    OPENFILENAME ofn;

    char szFileName[MAX_PATH] = "";

    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize = sizeof(ofn); 
    ofn.hwndOwner = NULL;
    ofn.lpstrFilter = (LPCWSTR)L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = (LPWSTR)szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = (LPCWSTR)L"txt";

    GetSaveFileName(&ofn);
    printf("the path is : %s\n", ofn.lpstrFile);
    getchar();
    return 0;
}

But the output is :

 the path is : H 

why ? Am I doing something wrong ?
I'm using Visual Studio 2008 on Windows 7.

like image 370
Hamed Avatar asked May 04 '12 13:05

Hamed


1 Answers

This line:

printf("the path is : %s\n", ofn.lpstrFile);

should use the wide char version of printf.

wprintf(L"the path is : %s\n", ofn.lpstrFile);
like image 78
Tony The Lion Avatar answered Sep 17 '22 20:09

Tony The Lion