473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to use swprintf() under linux in C?

5
I tried my best to use swprintf() - but I don't seem to grasp what I'm doing wrong. The following sample would print 1, instead of the length of "Hello" - 5. (There are no compiler errors or warnings)

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wchar.h>
  4.  
  5. int main(void)
  6. {
  7.   wchar_t *wstr;
  8.  
  9.   if ((wstr = malloc((wcslen(L"Hello") * sizeof(*wstr)) +1))
  10.         == NULL) {
  11.     printf("malloc returned NULL");
  12.     return 1;
  13.   }
  14.   if (swprintf(wstr, wcslen(L"Hello") +1 ,L"%s", L"Hello")
  15.        == -1) {
  16.     printf("swprintf returned -1");
  17.     return 1;
  18.   }
  19.   printf("%d\n",wcslen(wstr));
  20.   return 0;
  21. }
  22.  
May 5 '10 #1
10 7353
RedSon
5,000 Expert 4TB
Try:
Expand|Select|Wrap|Line Numbers
  1. #define MY_NAME L"RedSon"
  2.  
  3. wchar_t name[50];
  4. swprintf(name, 50, L"Hi my name is %s", MY_NAME);
  5.  
May 5 '10 #2
RooIt
5
Thanks for the response RedSon, but unfortunately when I execute your sample it prints 15, isn't the correct amount of wide-characters - 20?
May 5 '10 #3
RedSon
5,000 Expert 4TB
When you use swprintf does the output show up the way you expect? What is the issue? The output of swprintf is wrong or the strlen output is not what you expect?
May 5 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
I ran this and it displays 5, which is the length of wstr.

I added a call to wprintf and it prints Hello.

So all this is correct and I guess I don't see your problem.

BTW: This only works in C. As coded, it won't compile in C++.
May 5 '10 #5
RooIt
5
@RedSon: The output was wrong, it showed: "Hi my name is R" , which made cwslen(name) print 15, instead of 20.

I managed to 'make it work', by replacing %s with a capital %S, now it prints the whole name, and cwslen(name) recognizes 20 wide-characters.

@weaknessforcats: Thanks for your input, I'm using gcc 4.4.0 under linux, can you think of a reason why I need to apply %S - while your attempt to use %s was perfectly valid?
May 5 '10 #6
RooIt
5
I just received an automated message: "Your post has been edited". The "Reason" field was empty, and I don't even know what been edited from my original post. Is there a way for me to find out what I been doing wrong, or what actually been edited?
(The message states: "This is an automated message, please do not reply.")
May 5 '10 #7
RedSon
5,000 Expert 4TB
@RooIt
Roolt, I wouldn't worry about that PM too much. It seems that if someone clicks edit by mistake and then doesnt change anything, then you get a PM. FYI, the edit and "reply" buttons are right next to eachother for moderators.
May 6 '10 #8
weaknessforcats
9,208 Expert Mod 8TB
This was the code I added:

Expand|Select|Wrap|Line Numbers
  1.  wprintf(L"%s\n", wstr);
I just made sure everything was in wchar_t.
May 6 '10 #9
weaknessforcats
9,208 Expert Mod 8TB
Also, I never heard of %S. All that's in the ANSI C book is %s. But then again, I am a C++ guy and not so much a C guy.
May 6 '10 #10
RooIt
5
@RedSon: Thanks for clarifying.

@weaknessforcats: Well, I guess I simply don't know to read. The manual states: "If an l modifier is present: The ''const wchar_t *'' argument is expected to be a pointer to an array of wide characters."

Which means my original mistake was, Instead of:
Expand|Select|Wrap|Line Numbers
  1. swprintf(wstr, wcslen(L"Hello") +1 ,L"%s", L"Hello")
  2.  
I should've:
Expand|Select|Wrap|Line Numbers
  1. swprintf(wstr, wcslen(L"Hello") +1 ,L"%ls", L"Hello")
  2.  
Now cwslen() returns 5 - which is the correct amount of wide-characters. Thanks a lot for your help guys, very appreciate it.
May 6 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jhuola Hoptire | last post by:
Just installed J2RE 1.4.2 on a Linux server. I am very knew to the POSIX world. I couldn't dig-up much in the docs or via google about the following: 1 - Is there a standard way to make sure...
0
by: Eric Raymond | last post by:
When installing the rpms (or the tar file) onto Red Hat Enteprise Linux AS Beta 1 (Taroon), we get the follwing error: Installing all prepared tables /usr/bin/mysql_install_db: line 1: 7690...
6
by: gnu | last post by:
Rationale to use Linux ======================= - I can't afford paying for $199 for the license of an OS that's arguably better thank Linux for each of 10 computers I have. - I want to be...
1
by: srihari | last post by:
Hai, I am trying to install IBM DB2 8.1 on Red Hat linux 8.0. My machine is Intel XEON 64bit. The installation went well except for the creation of tools catalog. When I tried to install the tools...
0
by: Zorba.GR | last post by:
IBM DB2 Connect Enterprise Edition v8.2, other IBM DB2 (32 bit, 64 bit) (MULTiOS, Windows, Linux, Solaris), IBM iSoft Commerce Suite Server Enterprise v3.2.01, IBM Tivoli Storage Resource Manager...
5
by: cranium.2003 | last post by:
hi, Here is my code #include <iostream.h> int main() { cout <<"HI"; return 0; } and using following command to compile a C++ program g++ ex1.cpp -o ex1
1
by: ig3v10 | last post by:
Hi , i have advice on the folloing code: wchar_t* day = new wchar_t ; swprintf(day,L"%4d-%2d-%2d",yy,mm,dd); how to make sure that my 'day' didn't get string bigger then 20 chars? found:
3
by: Rui Maciel | last post by:
I've just noticed that the swprintf() function is declared differently in some platforms. As far as I know, GCC declares that function as int swprintf(wchar_t *s, size_t n, const wchar_t *format,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.