How to use swprintf() under linux in C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RooIt
    New Member
    • May 2010
    • 5

    #1

    How to use swprintf() under linux in C?

    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)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <wchar.h>
    
    int main(void)
    {
      wchar_t *wstr;
    
      if ((wstr = malloc((wcslen(L"Hello") * sizeof(*wstr)) +1))
            == NULL) {
        printf("malloc returned NULL");
        return 1;
      }
      if (swprintf(wstr, wcslen(L"Hello") +1 ,L"%s", L"Hello")
           == -1) {
        printf("swprintf returned -1");
        return 1;
      }
      printf("%d\n",wcslen(wstr));
      return 0;
    }
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Try:
    Code:
    #define MY_NAME L"RedSon"
    
    wchar_t name[50];
    swprintf(name, 50, L"Hi my name is %s", MY_NAME);

    Comment

    • RooIt
      New Member
      • May 2010
      • 5

      #3
      Thanks for the response RedSon, but unfortunately when I execute your sample it prints 15, isn't the correct amount of wide-characters - 20?

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        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?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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++.

          Comment

          • RooIt
            New Member
            • May 2010
            • 5

            #6
            @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.

            @weaknessforcat s: 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?

            Comment

            • RooIt
              New Member
              • May 2010
              • 5

              #7
              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.")

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Originally posted by RooIt
                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.")
                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.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  This was the code I added:

                  Code:
                   wprintf(L"%s\n", wstr);
                  I just made sure everything was in wchar_t.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    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.

                    Comment

                    • RooIt
                      New Member
                      • May 2010
                      • 5

                      #11
                      @RedSon: Thanks for clarifying.

                      @weaknessforcat s: 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:
                      Code:
                      swprintf(wstr, wcslen(L"Hello") +1 ,L"%s", L"Hello")
                      I should've:
                      Code:
                      swprintf(wstr, wcslen(L"Hello") +1 ,L"%ls", L"Hello")
                      Now cwslen() returns 5 - which is the correct amount of wide-characters. Thanks a lot for your help guys, very appreciate it.

                      Comment

                      Working...