CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2011
    Posts
    72

    writing text to a file

    Windows XP Pro, Visual Studio 2008, C++, MFC

    When writing text to a file it ignores the \n, %d, and puts spaces between all the characters.
    Here is the open code:
    Please excuse typos because I cannot cut and paste from my work computer to here.

    Code:
    m_log_handle = CreateFile(
       L"C:\\TEMP\\log_file.txt",
      FILE_WRITE_DATA,
      FILE_SHARE_WRITE,
      NULL,
      FILE_ATTRIBUTE_NORMAL,
      NULL );
    An example of write code is:
    Code:
    CString file_string.format( L"\nport is %d", port_number );
    int length = file_string.GetLength();  // Length looks good
    WriteFile(  m_log_handle,
                     file_string,
                     length,
                     mp_bytes_written,
                    NULL );
    The debugger shows file_string contains "xport is 49000"
    But replace the leading x with a little graphics square.

    The port number is completely missing and the \n is displayed as a little box. No new line. I expect to see the little graphics symbol in the debugger, but not in the text file.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: writing text to a file

    Is your build a UNICODE?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Posts
    72

    Re: writing text to a file

    I think it is. When I look in the somewhere in project properties there is a field that says:

    Character Set Use Unicode Character Set.

    What would the answer be for Yes and for No.

    This is a large MFC project that will never leave a closed in laboratory. Is it difficult to change to non-unicode?

    Thanks for your reply.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: writing text to a file

    No it's not difficult to change but since you have shown strings that's specified as L"xxx" I guess there are a lot of them and you have to find them all and change to non-unicode or _T"xxx".

    How do you open the file that's created? I checked CreateFile at msdn but couldn't find anything that indicates that it's capable of handling the translation of \n into \r\n that is normal in windows text-files.

    Opening it in MSVC should work since it's capable of handling files with *nix line endings (I've never tried unicode files though)
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Nov 2011
    Posts
    72

    Re: writing text to a file

    The output file is written to disk as a .txt file and I just double click it to open notepad or wordpad. Is there a way to tell the write utility to not use unicode.

    And what about the number. The value is 49000 so why don't I see "4 9 0 0 0" in the output? The debugger shows the characters "49000" in the string.

    I would like to not use unicode, but this application interfaces with another application that I cannot change. I suspect that application uses unicode.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: writing text to a file

    Quote Originally Posted by bkelly View Post
    The output file is written to disk as a .txt file and I just double click it to open notepad or wordpad. Is there a way to tell the write utility to not use unicode.
    You use the proper non-Unicode version of the functions you're calling. In other words "CreateFileA" instead of CreateFile. From MSDN:

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    Unicode and ANSI names
    CreateFileW (Unicode) and CreateFileA (ANSI)
    And what about the number. The value is 49000 so why don't I see "4 9 0 0 0" in the output? The debugger shows the characters "49000" in the string.
    That is because the debugger is smart and knows how to display Unicode characters. If you want the raw byte version of the string, use the Memory window to view the data.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Nov 2011
    Posts
    72

    Re: writing text to a file

    I think that is what I am looking for. I will code it up tomorrow.
    Thank you for taking the time to post.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: writing text to a file

    Quote Originally Posted by bkelly View Post
    I think it is. When I look in the somewhere in project properties there is a field that says:

    Character Set Use Unicode Character Set.
    ...
    Then your code seems to be incorrect:
    Quote Originally Posted by bkelly View Post
    Code:
    CString file_string.format( L"\nport is %d", port_number );
    int length = file_string.GetLength();  // Length looks good
    WriteFile(  m_log_handle,
                     file_string,
                     length,
                     mp_bytes_written,
                    NULL );
    The third parameter of WriteFile is the number of bytes to write while you are passing in the number of wide characters in the CString which is twice less!
    Victor Nijegorodov

  9. #9
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: writing text to a file

    The problem is not whether the program is Unicode or not, it's just that you are writing only a line feed (\n) to the file:
    Code:
    CString file_string.format( L"\nport is %d", port_number );
    For Windows, text files should have a carriage return (\r) and line feed (\n) at the end of each line.

    Some methods allow the automatic translation when the file is written, but CreateFile does not give that option, so you have to do it yourself. Simply replace your code above with
    Code:
    CString file_string.format( L"\r\nport is %d", port_number );
    Hope this helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  10. #10
    Join Date
    Nov 2011
    Posts
    72

    Re: writing text to a file

    Quote Originally Posted by Paul McKenzie View Post
    You use the proper non-Unicode version of the functions you're calling. In other words "CreateFileA" instead of CreateFile. From MSDN:

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    That is because the debugger is smart and knows how to display Unicode characters. If you want the raw byte version of the string, use the Memory window to view the data.

    Regards,

    Paul McKenzie
    I switched from CreateFile to CreateFileA and did not see any difference at all.

  11. #11
    Join Date
    Nov 2011
    Posts
    72

    Re: writing text to a file

    Enough! Make that too much!

    I switched to the old concept:

    char debug_text[ MAX_SIZE ];
    sprintf( debug_text, "text %d", number );

    and when the compiler fussed at me, switched again to:

    sprintf_s( debug_text, MAX_SIZE, "\r\n text %d", number );

    Suprisingly, the writefile function accepts the char *.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured