Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:30:09+00:00 2026-06-18T04:30:09+00:00

I am trying to get simple file IO working in Win32. So far the

  • 0

I am trying to get simple file IO working in Win32. So far the writing is working fine, but the reading is not: although it successfully reads the contents, additional “garbage” is appended to the string. The code I have so far is below. The program has UNICODE defined.

For writing:

DWORD dwTextSize = GetWindowTextLength(hWndTextBox);
WCHAR *lpszText = new WCHAR[dwTextSize];
GetWindowText(hWndTextBox, lpszText, dwTextSize + 1);
hTextFile = CreateFile(lpszTextFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwBytesWritten;
WriteFile(hTextFile, lpszText, 2 * dwTextSize, &dwBytesWritten, NULL);  // x2 for 2 bytes per Unicode character
CloseHandle(hTextFile);
DeleteObject(hTextFile);

For this example, Hello, World! is saved successfully as Hello, World!.

For reading:

lpszTextFileName = L"text.txt";        // LPCTSTR Variable
hTextFile = CreateFile(lpszTextFileName, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwFileSize = GetFileSize(hTextFile, &dwFileSize);
DWORD dwBytesRead;
WCHAR *lpszText = new WCHAR[dwFileSize / 2];
ReadFile(hTextFile, lpszText, dwFileSize, &dwBytesRead, NULL);
CloseHandle(hTextFile);

The string is then used to set the text of an EDIT control:

SendMessage(hWndTextBox, WM_SETTEXT, NULL, (LPARAM)lpszText);    // SetWindowText() also possible

When Hello, World! is read back in, it reads back in as Hello, World!﷽﷽ꮫꮫꮫꮫﻮ or a visual variation upon this, but basically “garbage”!

I have probably missed something rather obvious, but I cannot see where! Is there a solution to this problem and if so, what is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:30:10+00:00Added an answer on June 18, 2026 at 4:30 am

    Ok I started this with a comment, but its getting out of control.

    For Writing

    This:

    WCHAR *lpszText = new WCHAR[dwTextSize];
    

    should be:

    WCHAR *lpszText = new WCHAR[dwTextSize+1];
    

    This:

    DeleteObject(hTextFile);
    

    should not be there at all.. Get rid of it.

    I’m assuming you delete [] lpszText; somewhere when you’re done with it. if not, do so.


    For Reading

    The second parameter to GetFileSize() should not be the same variable as your return value. It is the HIGH 32bit of a 64-bit value for large file sizes. If you know you’re file size is smaller than 4gB, you can leave it NULL, so change this:

    DWORD dwFileSize = GetFileSize(hTextFile, &dwFileSize);
    

    to this:

    DWORD dwFileSize = GetFileSize(hTextFile, NULL);
    

    You must account for the null terminator of your file buffer, so this:

    WCHAR *lpszText = new WCHAR[dwFileSize / 2];
    

    should be changed to this:

    WCHAR *lpszText = new WCHAR[dwFileSize / 2 + 1];
    lpszText[dwFileSize / 2] = 0;
    

    and the rest should work as you’re hoping it would. No error checking, which is not good, but I’ve seen worse. And as before, I’m assuming you delete [] lpszText; somewhere when you’re done with it. if not, do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a simple send and receive UDP program working, but I'm
I'm trying to get a simple file upload working using jquery-fileupload-rails carrierwave fog I
I have a very simple properties file test I am trying to get working:
I'm trying to get this simple program to work on windows, but it crashes:
I'm trying to get a simple winsock program working, so I create my socket
I'm trying to get a simple VBO running. But when i call glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*vertexcount,
So i am trying to get a simple system working where i have an
I have been trying to get a simple spider to run with scrapy, but
I am trying to get a simple example working from an assembly book I
I am trying to get a simple example of a Atmosphere chat program working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.