sprintf

Calling swprint from a separate lib fails

Hi all I am facing a strange problem. I am using sprintf or swprintf according to the build defines with or without unicode. I have wrapped these functions in my own function like this: int mysprintf( MCHAR* str,size_t size, const MCHAR* format, ... ) { #ifdef MYUNICODE return swprintf( str, size, format); #else return snprintf...

Using floats with sprintf() in embedded C

Guys, I want to know if float variables can be used in sprintf() function. Like, if we write: sprintf(str,"adc_read = %d \n",adc_read); where adc_read is an integer variable, it will store the string "adc_read = 1023 \n" in str (assuming that adc_read = 1023) How can I use a float variable in place of integer? ...

c++ what happens if you print more characters with sprintf, than the char pointer has allocated?

Hello, I assume this is a common way to use sprintf: char pText[x]; sprintf(pText, "helloworld %d", Count ); but what exactly happens, if the char pointer has less memory allocated, than it will be print to? i.e. what if x is smaller than the length of the second parameter of sprintf? i am asking, since i get some strange behaviour ...

Rounding doubles - .5 - sprintf

I'm using the following code for rounding to 2dp: sprintf(temp,"%.2f",coef[i]); //coef[i] returns a double It successfully rounds 6.666 to 6.67, but it doesn't work properly when rounding 5.555. It returns 5.55, whereas it should (at least in my opinion) return 5.56. How can I get it to round up when the next digit is 5? i.e. return...

Displaying information from MATLAB without a line feed

Is there any way to output/display information from a MATLAB program without an ending line feed? My MATLAB program outputs a number a bit now and then. Between outputting the number the program does a lot of other stuff. This is a construct mainly to indicate some kind of progress and it would be nice not to have a line feed each time,...

PHP sprintf() and printf() functions

I've never used these functions before but after reading a lot about sprintf(), I decided I should get to know it. So I went ahead and did the following. function currentDateTime() { list($micro, $Unixtime) = explode(" ",microtime()); $sec= $micro + date("s", $Unixtime); $sec = mb_ereg_replace(sprintf('%d', $sec), "", ($micro + d...

Padding a time on the left with sprintf ?

Hi, I'm trying to pad a time (playtime of an MP3) using sprintf() in PHP. sprintf("%02d:2d:2d", $time); The function that returns the time gets, for example, '1:56' if the MP3 is 1 minute 56 seconds long, and that call brings back "01:56:00" (whereas it needs to be 00:01:56). How can I make this work? How can I tell sprintf to pad ...

Printing the value of a float to 2 decimal places

I have a float with the value of e.g 57.400002. I use sprintf_s to display the value on my GUI. sprintf_s(xPosition, 19, "%f", xPositionValue); How can I format the float so it displays as 57.40? ...

Is sprintf(buffer, "%s […]", buffer, […]) safe?

I saw use of this pattern to concatenate onto a string in some code I was working on: sprintf(buffer, "%s <input type='file' name='%s' />\r\n", buffer, id); sprintf(buffer, "%s</td>", buffer); and I'm fairly certain it's not safe C. You'll notice that buffer is both the output and the first input. Apart from the obvious possibility o...

Microsoft _stprintf warning

Why I get the following warning for the following code :) Code: _stprintf(m_szFileNamePath,_T("%s"),strFileName); warning C4996: '_swprintf': swprintf has been changed to conform with the ISO C standard, adding an extra character count parameter. To use traditional Microsoft swprintf, set _CRT_NON_CONFORMING_SWPRINTFS. I know _s...

Why use sprintf function in PHP?

I am trying to learn more about the PHP function sprintf() but php.net did not help me much as I am still cofused, why would you want to use it? Take a look at my example below... Why use this... $output = sprintf("Here is the result: %s for this date %s", $result, $date); When this does the same and is easiar IMO to write... ...

I'm new to C, can someone explain why the size of this string can change?

I have never really done much C but am starting to play around with it. I am writing little snippets like the one below to try to understand the usage and behaviour of key constructs/functions in C. The one below I wrote trying to understand the difference between char* string and char string[] and how then lengths of strings work. Furth...

sprintf() gone crazy

I need some help with this, since it baffles me in my C program I have 2 strings(base, and path) BASE: /home/steve/cps730 PATH: /page2.html this is how printf reads then just before I call a sprintf to join their content together. here is the code block int memory_alloc = strlen(filepath)+1; memory_alloc += strlen(B...

sprintf_s with a buffer too small

The following code causes an error and kills my application. It makes sense as the buffer is only 10 bytes long and the text is 22 bytes long (buffer overflow). char buffer[10]; int length = sprintf_s( buffer, 10, "1234567890.1234567890." ); How do I catch this error so I can report it instead of crashing my application? Edit: ...

Sprintf in Ruby

Sort of a quick question. I'm writing: puts "%.3f %.4f %.5f" % [3.998877, 3.998877, 3.998877] and get the following output: 3.999 3.9989 3.99888 sprintf simply rounds the numbers. How do I restrict that rounding? ...

Why do I get a trailing '1' after Perl's printf output?

When I implement the code below I get the correct dates: 10/05/2008 10/05/2009 When I use printf instead of sprintf, I get the following: 10/05/200910/05/20081 1 Any ideas on why printf prints the trailing 1? #!/usr/bin/perl use strict; use warnings; my ($from_date, $to_date) = to_from_dates(); print "$from_date\n"; print "$t...

Passing variable argument list to sprintf()

I would like to write a function that (amongst other things) accepts a variable number of arguments and then passes them to sprintf(). For example: <?php function some_func($var) { // ... $s = sprintf($var, ...arguments that were passed...); // ... } some_func("blah %d blah", $number); ?> How do I do this in PHP? ...

Why does Perl's sprintf not round floating point numbers correctly?

I was out looking for the rounding convention used by Perl's built-in function sprintf. I was thinking that it does a normal rounding (e.g. ROUND_HALF_UP as in Java's rounding mode convention), but digging further proved this to be wrong: > /usr/local/bin/perl5.10.1 -e 'print(sprintf("%.2f", shift @ARGV)."\n");' 0.335 0.34 > /usr/local...

What is the minimum buffer size for sprintf with %g?

The problem is to statically allocate a buffer large enough to fit a printed double, formatted with %g at maximum precision. This seems like a simple enough task, bu I'm having trouble. The best I have come up with (assuming the number to be printed is x) is char buf[1 + DBL_DIG + DBL_DIG + 1 + 1 + 1 + DBL_DIG + 1]; int len = sprintf(...

sprintf() to truncate and not round a float to x decimal places?

Hi folks, When calculating a golf handicap differential you are supposed to truncate the answer to 1 decimal place without rounding. No idea why but... I know how to do this using TRUNCATE() in mySQL SELECT TRUNCATE( 2.365, 1 ); // outputs 2.3 but I was wondering if sprintf() could do this? The only way I know to work with decim...