Writing a string to file using WinAPI

This is a discussion on Writing a string to file using WinAPI within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I'm new to windows programming and I'm having problems figuring out the 'proper windows datatype' to use for ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    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 :

    Code:
        
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02:02 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21