LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2009, 12:11 PM   #1
shogun1234
Member
 
Registered: May 2004
Posts: 226

Rep: Reputation: 15
newbie question about wprintf


I try to print wide character string by following article at http://www.linux.com/feature/51836; but it fails.

It seems the fwide(stdout, 0) always won't be 0. What should I do if I want to print wide character string to the terminal (e.g. gnome-terminal)?

gcc version 4.3.2 (Debian 4.3.2-1.1)

Thanks,

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

#include <wchar.h>

int main(int argc, char **argv)
{
        char *mbs;
        wchar_t *wcs;
        size_t size_wcs;

        setlocale(LC_CTYPE, "");

        mbs = malloc(sizeof(mbs));
        wcs = malloc(sizeof(mbs));

        strcpy(mbs, "This is 文件 vi 打的.");

        size_wcs = mbstowcs(wcs, mbs, strlen(mbs));
        printf("size of wcs: %d\n", size_wcs);

        if(fwide(stdout, 0) == 0){
                if(fwide(stdout, 1) <= 0)
                        printf(" Not in wide character mode!\n");
                else
                        wprintf(L" In wide character mode.  wcs: %ls\n", wcs);
        }else{
                printf("Not in wide character mode!\n");
        }
        return 0;
}
 
Old 02-09-2009, 06:52 AM   #2
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by shogun1234 View Post
It seems the fwide(stdout, 0) always won't be 0. What should I do if I want to print wide character string to the terminal (e.g. gnome-terminal)?
You should be able to print wide strings using one of printf's (not wprintf) format - it should be "%S", I think, but I'm not sure, because it is different across windows/linux, so check printf man page.
I'd recommend to try wprintf without using "fwide" check and see what happens.

Side note:
Quote:
Originally Posted by shogun1234 View Post
Code:
        strcpy(mbs, "This is 文件 vi 打的.");
I wouldn't do that.
Although you correctly set locale, there is no warranty it will be utf8 on any system where program will be run. I'd recommend to either store string as wide chars (L"This is 文件 vi 打的.") or set locale to the one required within program(for example, en_US.UTF-8) instead of system default.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
question: portable char* format specifier for wprintf() ??? maxreason Programming 1 01-20-2009 07:14 PM
Apache newbie question.. (very newbie question) tarballed Linux - Newbie 1 02-07-2003 08:41 PM
RE: Suse 8.0 hardware question {newbie question, pls help} Radiouk Linux - Distributions 2 06-04-2002 12:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration