LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

snprintf behaviour

Hello,

 

I just noticed that some time ago the snprintf and vsnprintf functions have been made available in CVI.

 

I thought it was good news, but the problem is that the functions don't behave the same way as those in msvcrt.dll:

 

#include <ansi_c.h>

int main(void)
{
    char prout[50];
    int err;
    double x = 0.12345;

    prout[0] = '\0';
    err = snprintf(prout, 1, "%e", x);


    printf("%i : %s.\n", err, prout);
}

Using the labwindows function, you will get err = 12.

Using the msvcrt.dll function, you would get err = -1 (you would also get -1 on linux or almost any other O/S I guess).

 

I would like to use the snprintf function labwindows provides (to get more checks during debug), but I think that would be a pain to do it in a (more or less) portable way.

What would be the best approach, considering I need to know if the write occurred ?

 

Best regards.

 

0 Kudos
Message 1 of 5
(3,869 Views)

12 and -1 have different meaning both according to CVI help and MSDN documentation: the latter indicates an error while the former is the lenght of the formatted string (could it be longer than allowed number of characters).



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?
0 Kudos
Message 2 of 5
(3,859 Views)

I think -1 would be the correct reeturn.

1 bytes is not wide enough to place the value of x in scientific notation (my guess for %e) into prout.

 

What is the minimum value for the error to disappear in those non-CVI compilers? 12 or 13?

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 5
(3,852 Views)

I don't think so: I have no compiler to actually test it against but according to the documentation -1 is returned only in case of encoding errors; if no error is raised, the function returns the lenght of the formatted output regardless the lenght of the target string. It's up to the user to compare the return value of the function with the parameter passed to it: see the examples here.

Again, I cannot test it on a compiler other than CVI, but the -1 output of msvcrt is more surprising to me than the 12 of CVI.

What does msvcrt return if passing e.g. count = 5?



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?
0 Kudos
Message 4 of 5
(3,847 Views)

I had a look to the man page under linux, and there is something that could explain the different behaviour between msvcrt/labwindows:

 

The glibc implementation of the functions snprintf() and vsnprintf() conforms to the C99 standard, that is, behaves as described above, since glibc version 2.1. Until glibc 2.0.6 they would return -1 when the output was truncated.

(from http://linux.die.net/man/3/snprintf)

 

So, it looks like the behaviour of getting -1 when truncated is not C99. So labwindows has a better behaviour (to my point of view) than msvcrt.

 

So now, I need to rewrite my code to take this into consideration.

 

0 Kudos
Message 5 of 5
(3,839 Views)