LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

sprintf ignores field width

I'm using sprintf as a clean way of copying four bytes of data from one array into another and placing a null terminator after them:

sprintf( dest, "%4s", cValueArray );

However, it seems to be not just copying four bytes from cValueArray, but instead copying up to where it finds a null terminator.  I found one post about a similar problem, that didn't seem to be answered: http://forums.ni.com/ni/board/message?board.id=180&message.id=11632&query.id=440390#M11632

Thanks in advance for any help.
-Nick
0 Kudos
Message 1 of 3
(3,326 Views)

Well, according to K&R the correct attribute to use is precision, not minimum field width. That is:
sprintf (dest, "%.4s", cValueArray );    (Please note the dot Smiley Wink )

copies exactly 4 bytes to destination string adding a nul terminator at the end.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 3
(3,316 Views)
Aha - my mistake.  Thanks very much!

It's worth noting that the field width, as I originally uses it, specifies the minimum amount of space to be taken up in the destination string by that conversion specifier (using spaces to pad on the left if the result of the conversion takes up fewer characters than specified by the given field width).
0 Kudos
Message 3 of 3
(3,298 Views)