Write a text file with Visual Studio

Hi, I am trying to write a function thats save to a text file the text I wrote in an EditBox of my main dialog. I tried many ways but the only approach that seems to work is through the code that I detail below. Still I could not write down the whole text, the last three characters are not written!!!, What I am doing wrong?. Is there any way of doing this more easily?. I am using Visual Studio C++ 2005 Express Edition and if i try to use fopen compiler tell me is a deprecated method, so I don't want to use it.

Thanks in advance.

HRESULT RecordAuxTextData(HWND hDlg)

{

HANDLE hDestFile = INVALID_HANDLE_VALUE;

BOOL bSuccess = FALSE;

// Open destination file

hDestFile = CreateFile (TEXT("c:\\RASITSonidos\\myfile.txt"), GENERIC_READ|GENERIC_WRITE, 0,

0, CREATE_ALWAYS, 0, 0);

if (INVALID_HANDLE_VALUE == hDestFile)

{

return FALSE ;

}

else

{

// Obtengo el contenido del textbox

DWORD dwTextLength;

HWND hEdit = GetDlgItem(hDlg, IDC_EDIT1);

dwTextLength = GetWindowTextLength(hEdit);

if(dwTextLength > 0)

{

LPWSTR pszText;

DWORD dwBufferSize = dwTextLength + 1;

pszText = (LPWSTR)GlobalAlloc(GPTR, dwBufferSize);

if(pszText != NULL)

{

if(GetWindowText(hEdit, pszText, dwBufferSize))

{

DWORD dwWritten;

if(WriteFile(hDestFile, pszText, dwBufferSize, &dwWritten, NULL))

bSuccess = TRUE;

}

//GlobalFree((HGLOBAL)pszText);

}

}

CloseHandle(hDestFile);

return S_OK;

}

}

[2358 byte] By [elpepe] at [2007-12-24]
# 1
pass into WriteFile() buffersize * sizeof(wchar_t)

Kuphryn

kuphryn at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 2
Thanks Kuphryn, still I don't understand. Do you mean replace dwBufferSize by sizeof(pszText)? or I must explicitelly write down a new buffersize* pointer variable? Sorry but I am newbie.
elpepe at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 3
Problem is that dwTextLength represents the number of characters from the string. But a character is 2 bytes wide with the UNICODE set (as in your case). Since WriteFile requires the number of bytes from the buffer you have to multiply the lenght of the string with the number of bytes of a character, dwTextLength * sizeof(TCHAR).
MariusBancila at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 4
AHHH, OK. I understand now. I change that and it works. Thanks!
elpepe at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 5

Hi, well, nost so good news. Now I am writing the file. The new problem is that in that way (*sizeof(TCHAR)) each character is written as 2 characters, the other is a white space. That is not what I a want to do. I thought the problem was that I am using LPWSTR instead of LPSTR (the original type of the Microsoft sample) but if I change my code as shown below I only get a compiling error:

error C2664: 'GetWindowTextW' : cannot convert parameter 2 from 'LPSTR' to 'LPWSTR'

This is my new code

if(dwText1Length > 0)

{

LPSTR pszText;

DWORD dwBufferSize1 = dwText1Length + 1;

pszText = (LPSTR)GlobalAlloc(GPTR, dwBufferSize1);

if(pszText != NULL)

{

if(GetWindowText(hEdit1, pszText, dwBufferSize1)) //Here's the error and the parameter with problems is pszText

{

DWORD dwWritten;

if(WriteFile(hDestFile, pszText, dwBufferSize1*sizeof(wchar_t), &dwWritten, NULL))

bSuccess = TRUE;

}

}

}

elpepe at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 6
windows api is one layer above c++. looks like you are mixing random code. should do i/o via c++ ofstream first.

Kuphryn

kuphryn at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 7
Thanks again kuphryn. I solved my problem changing from GatWidowText function to GetWindowTextA (the first one it is the same as GetWindowTextW).
elpepe at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...