Thread: storing integer values in files using WriteFile

  1. #1
    Registered Trademark
    Join Date
    Apr 2005
    Posts
    19

    storing integer values in files using WriteFile

    for my latest project, i need to store coordinate data inputted from the user into a file using WriteFile. However, when i try to cast the numbers to characters/string (as they need to be for file storage), it won't work and neither will the program. please help!
    pardon the double post. My browser didn't react quickly for DSL and i thought i didn't click the button properly

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It would have been better to see your code first, but how about this:
    Code:
    int num = 0xABCDEFFF;
    WriteFile(hFile,(char*)(&num),sizeof(num),&written,NULL);
    //or if you're using C++ you might prefer C++-style cast
    WriteFile(hFile,reinterpret_cast<char*>(&num),sizeof(num),&written,NULL);
    That should work unless you want to be able to read what the number is if you open it as plain text. In that case, you'd need to convert the number to a string first (see the faq)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Storing input values while iterating
    By russel1013 in forum C Programming
    Replies: 11
    Last Post: 07-29-2008, 08:32 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. Storing images or files inside executable?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-30-2002, 08:31 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM