Find the answer to your Linux question:
Results 1 to 4 of 4
Code: int main (int argc, char *argv[]) { printf("printf1\n"); wprintf(L"wprintf\n"); printf("printf2\n"); return(0); } Output of this program is Code: printf1 printf2 I am using gcc 4.0.2 on SUSE 10.0. The ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2005
    Posts
    5

    Question Can't mix printf with wprintf?


    Code:
    int main (int argc, char *argv[])
    {
       printf("printf1\n");
       wprintf(L"wprintf\n");
       printf("printf2\n");
       return(0);
    }
    Output of this program is
    Code:
    printf1
    printf2
    I am using gcc 4.0.2 on SUSE 10.0.

    The GNU C Library Section 12.6 Streams in Internationalized Applications, it clearly states that you can not mix these two. Or am I missing something?
    It is important to never mix the use of wide and not wide operations on a stream. There are no diagnostics issued. The application behavior will simply be strange or the application will simply crash.
    Now I can understand this might be an issue for other streams but now we are talking about console output.

    I have several questions.
    . Am I missing something?
    . Is this a known issue? I never heard something like this before, and google returns nothing useful.
    . Is there a workaround? A compiler switch, a environmental variable to mess with, something?
    How do people solve this issue when using several different libraries and each using a diffrent (w)printf and/or (w)cout?

    Thanks.

  2. #2
    Just Joined!
    Join Date
    Mar 2006
    Posts
    34
    I suggest this workaround:

    Code:
    int main() {
        wprintf(L"%s", "printf1\n");
        wprintf(L"%ls", L"wprintf\n");
        wprintf(L"%s", "printf2\n");
    }
    This does the right thing on my system (glibc 2.3.5-r2 and gcc 3.3.5):
    Code:
    printf1
    wprintf
    printf2
    Or am I missing something, now?

  3. #3
    Just Joined!
    Join Date
    May 2005
    Posts
    5
    If you always use wprintf then yes. It works. But the problem is you can not always control that.

    The most simple case I can think of is ...
    Lets say I am using a third party library, that has nothing to do with wprintf, and this library is using printf (or cout, whatever) for their ... say debug output.

    Now my application, I have to use wprintf, there is no way around it. Now either my output or theirs are blocked. I have to convert the stream (in this case the console) using fwide (or whatever) everytime I call a function in that third party library, whether it is output releated or not, because they might use printf.

  4. #4
    Just Joined!
    Join Date
    Mar 2006
    Posts
    34
    I don't see any obvious solution to the problem, in that case -- other than calling fwide every time you use the third-party library, of course. Sorry.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •