wsprintf() Function how to read Double Variable
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: wsprintf() Function how to read Double Variable

  1. #1
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    wsprintf() Function how to read Double Variable

    I have one problem with wsprintf Function
    if I write this code :
    Code:
    int I =500  ;int S ;
    WCHAR sBuffer[10];
    S= wsprintf(sBuffer,TEXT("  I value :  %i  "), I);   // Here is the problem
    Now My Problem is , If The Varibale I is double Not integer , How can I let wsprintf read the double variable

    Simply if I have I = 122.45566777776 as Example , how can I print by using wsprinf function.

    I think I have to convert I number (122.45566777776) to WCHAR varaible then print directly
    But this will be very long way if I have to convert each int variable to wchar variable.



    Kindly advise , and provide code examples if possible.

    Thanks & Regards

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: wsprintf() Function how to read Double Variable

    1. wsprintf() does not read, it writes (prints).
    2. Correct format specifier for double in printf
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: wsprintf() Function how to read Double Variable

    wsprintf is an old remnant from the 16-bit era that, in order to have low footprint and be faster than sprintf, not supports floating point numbers. It also has a buffer size limit of 1024 bytes. See http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    You should use one of the other formatting functions available like sprintf (or variants of it) or std::stringstream.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: wsprintf() Function how to read Double Variable

    Quote Originally Posted by S_M_A View Post
    wsprintf is an old remnant from the 16-bit era that, in order to have low footprint and be faster than sprintf, not supports floating point numbers. It also has a buffer size limit of 1024 bytes. See http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Wow!
    Very nice to learn again something new!
    Frankly, I NEVER used wsprintf(). Ever. Since the most of my projects are MFC ones I also very rarely use sprintf.
    So, sorry for the incorrect reply!
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: wsprintf() Function how to read Double Variable

    Frankly, I can't tell how happy I am learning you something new. Such opportunities are at the best very rare...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  6. #6
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: wsprintf() Function how to read Double Variable

    Thanks so much S_M_A and VictorN for valuable answers,

    and Here is code example :
    Code:
     int p,z;
    double x;    x = 23.4454          // as example
    
     CHAR AA[20];
     TCHAR sBuffer[80];
    z= sprintf_s(AA, " %f",x);      	// Note sprintfl not working with win7_64  MSDN Recommend sprintf_s							 
     p=wsprintf(sBuffer,TEXT("%S"),AA);  // S should be Capital
    x=_wtof(sBuffer);                           // Now again we con convert TCHAR to Double by using _wtof function

  7. #7
    Join Date
    Nov 2003
    Posts
    1,819

    Re: wsprintf() Function how to read Double Variable

    >> // S should be Capital
    You should use "%hs". I recommend that you forget that %S even exists - use %s, %ls, or %hs.

    >> // Now again we convert TCHAR to Double by using _wtof function
    You should use _ttof().

    gg

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center