3

Here is my simple C/C++ program:

int main() {
  FILE* f = fopen("MyTest.log", "w");

  fprintf(f, "%ls\n", L"abc");
  fprintf(f, "%ls\n", L"您好"); // Data from a different locale
  fprintf(f, "%ls\n", L"def");
  fclose(f);

  return 0;
}

When I run this program, the generated file does not contain the middle line at all. It appears fprintf simply returns back once it detects a different character set in the passed parameter.

I have tested this program on Windows as well as Ubuntu. The same problem at both the places.

Browsing the forum, I found a few hints such as using _setmode() and fwprintf. However, if possible, I would like to stick to fprintf(). Also, my code needs to work on Windows as well as Linux.

Does anyone know to achieve this? Regards.

Peter
  • 10,409
  • 14
  • 70
  • 136

1 Answers1

0

I think you need to set locale first to get it work properly..

See this question for more info

Why doesn't printf format unicode parameters?

you can use UTF-8 char set if your characters are there in this...

Printing UTF-8 strings with printf - wide vs. multibyte string literals

Community
  • 1
  • 1
smali
  • 4,499
  • 6
  • 35
  • 57