Reading Unicode C ++ Files

I have a simple question. I have a UTF 16 text file to read that starts with FFFE. What are the C ++ tools for working with this file? I just want to read it, filter out some lines and display the result.

It looks simple, but I have experience with regular ascci files, and I'm in a hurry. I am using VS C ++, but I do not want to work with managed C ++.

Hi

Here is a very simple example.

wifstream file; file.open("C:\\appLog.txt", ios::in); wchar_t buffer[2048]; file.seekg(2); file.getline(buffer, bSize-1); wprintf(L"%s\n", buffer); file.close(); 
+7
c ++ file visual-c ++ unicode utf-16
source share
4 answers

You can use fgetws , which reads 16-bit characters. Your file is in byte order. Since x86 machines are also not very similar, you should be able to process the file without any problems. When you want to make a conclusion, use fwprintf .

In addition, I agree that additional information may be helpful. For example, you can use a library that abstracts part of this.

+2
source share

For what it's worth, I think I read that you should use the Microsoft feature, which allows you to specify the encoding.

http://msdn.microsoft.com/en-us/library/z5hh6ee9(VS.80).aspx

+1
source share

As you hurry, use ifstream in binary mode and do your job. I had the same problems with you and it saved my day. (this is not a recommended solution, of course, just hack it)

  ifstream file; file.open("k:/test.txt", ifstream::in|ifstream::binary); wchar_t buffer[2048]; file.seekg(2); file.read((char*)buffer, line_length); wprintf(L"%s\n", buffer); file.close(); 
+1
source share

FFFE is just the initial specification (bytes). Just read the file as usual, but in a large char buffer.

0
source share

All Articles