Home > Enterprise >  Writing DWORD to end of file on Windows
Writing DWORD to end of file on Windows

Time:11-02

I am trying to figure out how to write a integer value to the end of my file. The value is size.

DWORD size = 12314432;
BOOL ret = WriteFile(hFile, size, sizeof(DWORD), NULL, NULL);

However WriteFile() requires that parameter 3 be of type LPCVOID so I am not sure how I would give it the DWORD instead.

I have tried..

unsigned char b[sizeof(DWORD)] = {0};
sprintf(b, "%d", size);
WriteFile(hFile, b, sizeof(DWORD), NULL, NULL);

However this just puts the hex value of each digit. So if size=1234 then it would write "31 32 33 44" to end of the file.

I would like the end of the file to just get the number in 4 bytes.

CodePudding user response:

You provide the address of the DWORD like this:

DWORD size = 12314432;
BOOL ret = WriteFile(hFile, &size, sizeof size, NULL, NULL);
  • Use the ampersand to get the address of the DWORD variable.
  • Express your intend for the size, you want to write all of size, so say so.

CodePudding user response:

You can pass (void*)&size. You do need something which has an address, like size. You can't pass an expression there. (void*) &(5*7 3) won't work.

  • Related