C ++, a function that loads text ignores the last few lines with only some .txt files

I am following the Forger Win API tutorial to load text files into an edit control. Sometimes the whole file loads correctly, and sometimes the last part is missing, where the "part" is in one case 2 and half lines and another 10 lines o_O. Here's what the files look like:

(I am a new user, therefore I do not allow me to post more than one hyperlink, therefore there is a gallery where there are screenshots: http://nancy.imgur.com/all/ and I mean the order in which they appear in the gallery)

2.5 lines left: second (reading stops at the cursor after "F")

10 lines left: fourth (also stops at the cursor after f)

Read more: first and third

I tried using fstreams instead, and the same stuff was left behind (I also could not get new line characters to be displayed in the edit = () control. Any idea what might be wrong?

I could not reference the Forger tutorial, so here's the function:

BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName) {
HANDLE hFile;
BOOL bSuccess = FALSE;

hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
    DWORD dwFileSize;

    dwFileSize = GetFileSize(hFile, NULL);
    if(dwFileSize != 0xFFFFFFFF)
    {
        LPSTR pszFileText;

        pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
        if(pszFileText != NULL)
        {
            DWORD dwRead;

            if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
            {
                pszFileText[dwFileSize] = 0; // Add null terminator
                if(SetWindowText(hEdit, pszFileText))
                    bSuccess = TRUE; // It worked!
            }
            GlobalFree(pszFileText);
        }
    }
    CloseHandle(hFile);
}
return bSuccess; }
+3
source share
3 answers

Is it possible that your text file contains embedded NUL characters, and therefore the line pszFileTextends earlier than you expect? The function SetWindowText()stops reading after it encounters the first NUL terminator.

, , dwRead , . , dwRead dwFileSize?

+1

SetWindowText strlen(pszFileText) dwFileSize. , \0.

0

There may be editing control restrictions. By default, the amount of text that a user can enter in edit mode is limited to 32 KB. What are the sizes of your text files?

0
source

Source: https://habr.com/ru/post/1779869/


All Articles