8

This just happened to me while testing a part of a bigger program that I isolated. The original function would remove non ascii characters from a string in a special manner that I needed, the thing is this program

#include <stdio.h>
#include <wchar.h>

int main(int argc, char *argv[])
{
    fwprintf(stdout, L"-- Example\n");
    fprintf(stdout, "-- Example\n");

    return 0;
}

would not print the second -- Example on my linux (Fedora 22) system. Although using fwprintf() again or fprintf(stderr, "-- Example\n"); would work.

  • Is this the expected behavior? And why?
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    What operating system? – trojanfoe Dec 17 '15 at 19:58
  • @trojanfoe Updated the tags. – Iharob Al Asimi Dec 17 '15 at 19:59
  • does work with MSVC. – Weather Vane Dec 17 '15 at 20:02
  • 1
    I think this issue might be related to the *wide orientation* of the file stream. If `fwide()` is not used then the orientation of the first file function is used. Once the stream is *wide* then you need to use *wide* functions. You could confirm this by reversing your use of `fprintf()` and `fwprintf()`. – trojanfoe Dec 17 '15 at 20:04
  • @trojanfoe I just tried `fwide(stdout, -1)` right before `fprintf(stdout, "-- Example\n");` and still doesn't work. Although your point does make sense, if `fwprintf()` does call `fwide()` probably `fprintf()` does it too. – Iharob Al Asimi Dec 17 '15 at 20:09
  • 1
    You cannot change it once it's been set. You need to be consistent with your use of (non-)wide functions on (non-)wide streams. – trojanfoe Dec 17 '15 at 20:09
  • @trojanfoe I Just read that in the manual page for `fwide()`. It is definitely this problem. You can answer that if you want. – Iharob Al Asimi Dec 17 '15 at 20:10

1 Answers1

2

I believe this issue might be related to the wide orientation of the file stream. If fwide() is not used then the orientation of the first file function is used to determine the orientation (you can also set the orientation when opening file, but that doesn't apply here).

Once the stream is wide then you need to use wide functions; and when non-wide you need to use non-wide functions.

Once the orientation is set, it cannot be changed.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • 1
    I'm reading your answer. I've tried to revert the wide direction and, as you say, it's not permitted to change it. I don't understand why this behaviour was imposed. Do you know the reason of this behaviour? – Sir Jo Black Jun 20 '17 at 12:52