Saving unicode strings to .txt files ..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Saving unicode strings to .txt files ..

  1. #1
    Join Date
    Jan 2012
    Location
    India
    Posts
    122

    Saving unicode strings to .txt files ..

    Hi ..

    In my project , we need to create an Array of Unicode Strings . The Array will contain 5000 Strings.

    I need to write those strings to a text file which can be opened or edited with NotePad.

    Normal _tfopen and fwrite are not able to create notepad compatible .txt file .. I mean the file I created is not

    readable with Notepad though file open mode is "w+t"

    How can I save my unicode strings to a text file

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,009

    Re: Saving unicode strings to .txt files ..

    You can use CStdioFile Class
    or Win32 API functions CreateFile + WriteFile
    Also consider inserting BOM in the beginning of the file.
    Victor Nijegorodov

  3. #3
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994

    Re: Saving unicode strings to .txt files ..

    Like Victor already did, I also suggest to use CStdioFile MFC class, that can make your life easier.
    However, if really-really need using CRT functions and no MFC...

    Notepad, as well as other text viewers/editors, uses BOM (byte order mark) to get the file text encoding.
    You can write it yourself or, better add ccs flag to fopen/_wfopen.

    Example
    Code:
        wchar_t* wchText = L"Ala bala portocala";
        FILE* f = _wfopen(L"c:\\test\\mytest.txt", L"w+t,ccs=UNICODE");
        if(NULL != f)
        {
            size_t size = fwrite(wchText, sizeof(wchar_t), wcslen(wchText), f);
            // ...
            fclose(f);
        }
    For more details, see Remarks under fopen, _wfopen MSDN topic.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,638

    Re: Saving unicode strings to .txt files ..

    CStdioFile is a bad idea if you need to create a file with UNICODE contents because it will always enforce ansi-mode. You could find yourself in a situation working around MFC more than working with MFC.

    either use CFile and do your own buffering and wide-string IO.
    or use _wfopen as Ovidu suggests

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,009

    Re: Saving unicode strings to .txt files ..

    Victor Nijegorodov

  6. #6
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994

    Re: Saving unicode strings to .txt files ..

    Quote Originally Posted by VictorN View Post
    PS: and this is a CStdioFile bug report in http://connect.microsoft.com/VisualS...text-correctly
    I found this issue has been resolved in VS2012.
    CFile::OpenFlags enum contains a new value, typeUnicode. So, to save unicode, you can do the following:
    Code:
        // ...
        UINT nOpenFlags = CFile::modeCreate | CFile::modeWrite;
    #ifdef _UNICODE
        nOpenFlags |= CFile::typeUnicode;
    #endif
    
        CStdioFile file(_T("d:\\test\\mytest.txt"), nOpenFlags);
        file.WriteString(_T("Ala bala portocala\r\n"));
        // ...
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  7. #7
    Join Date
    Jan 2012
    Location
    India
    Posts
    122

    Re: Saving unicode strings to .txt files ..

    Thanks thanks thanks to all of you ..

    This site has supported me many times ..

    I will read every thing carefully and will write down the program.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center