How do I write an int value with WriteFile()? (wapi)

Started by
7 comments, last by TDragon 21 years, 10 months ago
Every time I try to pass an int value into the buffer of WriteFile(), I get an error. (GetLastError() just returns the number 87). Here''s my code: WriteFile((HANDLE)info, (LPVOID)sprites[2].typenum, sizeof(int), &written, NULL); info is an HFILE, typenum is an int member of an array of a certain struct, and written is a DWORD. I have successfully written char values to the file, but cannot write any int values. Can WriteFile() not be used for that? Twilight Dragon www.freewebz.com/j-world
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Advertisement
Convert it to a string first.

Gamer-Insight.com
Ah. So that would mean that WriteFile() can''t handle int values? In that case, is there any function that can? Otherwise this file will end up taking up a whole lot more space than I''d like. And of course anyone will be able to open it up in Notepad and read it.

Twilight Dragon
www.freewebz.com/j-world
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Just pass a pointer to the integer, you don''t need to convert it to a string. May I ask _why_ you''re using the Win32 file I/O routines when both C and C++ supply a set of _standard_ functions/classes to do the same thing?

"Pass a pointer to the integer" --could you show me in code? I don''t quite understand what you mean by that. And since my program is a windows program, I naturally assumed I should use the windows functions, being more familiar with them than with dos functions.

Twilight Dragon
www.freewebz.com/j-world
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
''Null and Void'' is right. You can write an int using a pointer to that int. On Windows 32 platforms and VC++, an int is 32 bits, which happens to be the length of a pointer.

int i = 0;

&i //this is the pointer to i

Also, check out the c++ io classes and MFC CFile class.
Okay, I think I get it. I don''t use MFC, but will consider using the std c stuff (fopen(), fwrite()).

Twilight Dragon
www.freewebz.com/j-world
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
If you look up the description for your error code, it says "The parameter is incorrect.", which means you''re probably passing an inappropriate value to the function. I think you want to take the address of typenum. Try this instead:

WriteFile((HANDLE)info, (LPVOID)&sprites[2].typenum, sizeof(int), &written, NULL);

Note that this writes the data in a binary format. You''d have to convert it to a string before it would be readable in notepad.

To my knowledge, Microsoft''s implementation of the standard library calls Win32 API functions. For example, fwrite probably just calls WriteFile. If you aren''t concerned about portability, the Win32 API offers features that you can''t take advantage of using the standard library.


Matt
Matt
Ok, I''ve finally figured it out, thanks to you guys. I have ended up with the fopen, fwrite, etc, just because they are the easiest to use and least picky about what I do with them. This was to write tile-based level files for a platform game, for those interested, so I didn''t need people to be able to read it or anything. Thanks again!

Twilight Dragon
www.freewebz.com/j-world
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++

This topic is closed to new replies.

Advertisement