7

I have a simple question to ask. I have a UTF 16 text file to read wich starts with FFFE. What are the C++ tools to deal with this kind of file? I just want to read it, filter some lines, and display the result.

It looks simple, but I just have experience in work with plain ascci files and I'm in the hurry. I'm using VS C++, but I'm not want to work with managed C++.

Regards

Here a put 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();
Andres
  • 3,324
  • 6
  • 27
  • 32

4 Answers4

2

You can use fgetws, which reads 16-bit characters. Your file is in little-endian,byte order. Since x86 machines are also little-endian you should be able to handle the file without much trouble. When you want to do output, use fwprintf.

Also, I agree more information could be useful. For instance, you may be using a library that abstracts away some of this.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • Yes, you said all, more information could be useful. I tried at msdn, Jeffrey Richter book, but it seems they don't have good examples when talking about this subject. Thanks – Andres May 23 '09 at 13:07
2

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

  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();
Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
1

For what it's worth, I think I've read you have to use a Microsoft function which allows you to specfiy the encoding.

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

Philipp
  • 48,066
  • 12
  • 84
  • 109
Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
0

The FFFE is just the initial BOM (byte order mark). Just read from the file like you normally do, but into a wide char buffer.

rtn
  • 127,556
  • 20
  • 111
  • 121
  • example code is always a good idea - how do you know how he normally reads a file? –  May 23 '09 at 12:53
  • While I agree with you Neil, Andres does say: "but I just have experience in work with plain ascci files." ;) – xian May 23 '09 at 13:08