Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TDragon

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

This topic is 5891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
"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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
''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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!