Thread: Writing a string to file using WinAPI

  1. #1
    Registered User
    Join Date
    Mar 2010

    Writing a string to file using WinAPI

    Hi all,

    I'm new to windows programming and I'm having problems figuring out the 'proper windows datatype' to use for a string. All the typedefs are confusing me...

    Right now I'm using :

    CHAR teststring[]= "Testing writing a string to file.\r\n";
    DWORD numBytes=0;
    BOOL wtf = WriteFile(testfile,&teststring,sizeof teststring, &numBytes, NULL);
    Is this the proper coding style and datatype to use in this instance? It is writing to the file but it's adding a control character "^@" at the end.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Use strlen() rather than sizeof

    Or rather, the UNICODE independent variant of strlen() which adjusts to how CHAR is declared.

    For info, sizeof() counts the bytes, whereas strlen counts the characters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String as Resource in txt file
    By KeithS in forum Windows Programming
    Replies: 15
    Last Post: 08-29-2009, 12:04 PM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM