how to save file with GetSaveFileName in win32?

c++, savefiledialog, winapi

Solution

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);

Problem

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.

Original source