开发者

Opening Unicode text files in C++ and displaying their contents

开发者 https://www.devze.com 2023-02-16 05:55 出处:网络
Currently I am attempting to open a text file that was saved in Unicode format, copy it\'s contents to a wstring, and then display it on the console.Because I am trying to understand more about workin

Currently I am attempting to open a text file that was saved in Unicode format, copy it's contents to a wstring, and then display it on the console. Because I am trying to understand more about working with strings and opening files, I'm experimenting with it in a simple program. Here is the source.

int main()
{
    std::wfstream myfile("C:\\Users\\Jacob\\Documents\\openfiletest.txt");
    if(!myfile.is_open())
    {
        std::cout << "error" << std::endl;
    }
    else
    {
        std::cout << "opened" << std::endl;
    }
    std::wstring mystring;
    myfile >> mystring;
    std::wcout << mystring << std::endl;
    system("PAUSE");
}

When I try to display it on the console it displays  ■W H Y when it should display WHY (really it's "WHY WONT YOU WORK", bu开发者_如何学Ct ill worry about why it's incomplete later I guess). In all honesty, using Unicode is not very important to me because this isn't a program that I will be selling (more for just my self). I do want to get familiar with it though because eventually I do plan on needing to knowledge of using Unicode in C++. I am also using boost file-system for working with directories and multithreading while using C++/cli for the GUI. My question(s): Should I really bother using Unicode if I don't need it at this point in time, If so how do I fix this problem, and are there and cross platform libraries for dealing with strings and files that use different Unicode encodings (windows with UTF-16 and Linux with UTF-32).

Also, any articles on Unicode in C++ or Unicode in general would be appreciated. Here is one that I found and it helped a little.The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Thanks.

EDIT: Here is another arcticle I just found that was useful Reading UTF-8 Strings with C++


That's a byte order mark. If you find one at the beginning of the file, just strip it.

And the spaces in between letters are probably because the console isn't very wide char friendly.


It displays just one word because myfile is a stream and operator>> extracts just one string separated by whitespaces from the stream. You might want to try the getline function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号