How to append a interger value to the end of a text file by using Writefile() API in C++?

Mayank Agarwal 21 Reputation points
2022-06-07T19:00:20.957+00:00

How to append a interger value to the end of a text file by using Writefile() API in C++?

WriteFile() API is looking for a buffer to write, but I have aa integer variable to write. How can I convert integer value to buffer, so that the Writefile writes into the text file.

Please suggest.
TIA

Developer technologies | C++
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-06-07T19:41:40.437+00:00

    You can format it.
    For example :

                        HANDLE hFileAppend = CreateFile(L"E:\\test1.txt", FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
                        if (hFileAppend != INVALID_HANDLE_VALUE)
                        {
                            DWORD dwPos = SetFilePointer(hFileAppend, 0, NULL, FILE_END);
                            WCHAR wsText[MAX_PATH] = L"";
                            wsprintf(wsText, L"%d", 2022);
                            DWORD dwBytesWritten;
                            WriteFile(hFileAppend, wsText, lstrlen(wsText) * sizeof(WCHAR), &dwBytesWritten, 0);
                            CloseHandle(hFileAppend);
                        }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.