CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    42

    Question Text File Writing

    Want to write a text file to save log data during app runtime. Grabbed this code from MSDN itself, and it doesnt compile.

    HANDLE hFile = CreateFile(_T("C:\\TEMP\\MyFile.txt"),
    GENERIC_WRITE, FILE_SHARE_READ,
    NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    AfxMessageBox(_T("Couldn't create the file!"));
    else
    {
    CFile myFile(hFile);
    static const char sz[] = "Hockey is best!";
    myFile.Write(sz, lstrlen(sz));
    myFile.Close();
    }

    This is the error
    'lstrlenW' : cannot convert parameter 1 from 'const char [16]' to 'LPCWSTR'

    What am I doing wrong?

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

    Re: Text File Writing

    Your project is set for Unicode, and the code you show is not. There's a setting in the project properties that lets you change that.

    Here's the offending line:
    Code:
    static const char sz[] = "Hockey is best!";
    You could change it to
    Code:
    static const wchar_t sz[] = _T("Hockey is best!");
    Hope that 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

  3. #3
    Join Date
    Jan 2009
    Posts
    42

    Exclamation Re: Text File Writing

    Thanks - no more error during compilation, but only getting garbage characters in the text file...How do I fix that?

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

    Re: Text File Writing

    How did you test your file to conclude it contains "garbage"?
    Victor Nijegorodov

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