sprintf

Looking for algorithm that reverses the sprintf() function output

I am working on a project that requires the parsing of log files. I am looking for an fast algorithm that would take groups messages like this: Input: The temperature at P1 is 35F. The temperature at P1 is 40F. The temperature at P3 is 35F. Logger stopped. Logger started. The temperature at P1 is 40F. and puts out something in th...

how to use "%f" to populate a double value into a string with the right precision

I am trying to populate a string with the double value using a sprintf like this sprintf(S, "%f", val); But the precision is being cut of to 6 decimal places. I need about 10 decimal places for the precision. Kindly tell me how that can be achieved. ...

How do I emulate Python's named printf parameters in Ruby?

In Python, you can do this: print "Hi! I'm %(name)s, and I'm %(age)d years old." % ({"name":"Brian","age":30}) What's the closest, simplest Ruby idiom to replicate this behavior? (No monkeypatching the String class, please.) EDIT: One of the really excellent benefits of this is that you can store the pre-processed string in a varia...

The origin of sprintf-style string formatting

The string formatting concept found in sprintf can be found in almost any language today (you know, smothering a string with %s %d %f etc. and providing a list of variables to fill their places). Which langugage was it originally that had a library function or language construct which offered this functionality? Please specify some ki...

How can I improve/replace sprintf, which I've measured to be a performance hotspot?

Through profiling I've discovered that the sprintf here takes a long time. Is there a better performing alternative that still handles the leading zeros in the y/m/d h/m/s fields? SYSTEMTIME sysTime; GetLocalTime( &sysTime ); char buf[80]; for (int i = 0; i < 100000; i++) { sprintf(buf, "%4d-%02d-%02d %02d:%02d:%02d", sysTime.wYear...

sprintf() and WriteFile() affecting string Buffer

I have a very weird problem which I cannot seem to figure out. Unfortunately, I'm not even sure how to describe it without describing my entire application. What I am trying to do is: 1) read a byte from the serial port 2) store each char into tagBuffer as they are read 3) run a query using tagBuffer to see what type of tag it is (bo...

sprintf in C#?

Is there something similar to sprintf() in C#? I would for instance like to convert an integer to a 2-byte byte-array. Something like: int number = 17; byte[] s = sprintf("%2c", number); ...

sprintf access violation

Hi, I have a problem with the following code: for(i = 0;(i - 1)< n;i++) { char* b; sprintf(b, "%d", i); } It compiles fine but when I run it it give me the infamous "0XC0000005 Access Violation" error. I have tried setting b to NULL, "", "0", 0 and a bunch of other stuff but then I get the "0XC0000005 Access Violation" error or "Expre...

sprintf() without trailing null space in C

Is there a way to use the C sprintf() function without it adding a '\0' character at the end of its output? I need to write formatted text in the middle of a fixed width string. ...

Silly sprintf() question

How do I add the SQL wildcard characters to this: sprintf("SELECT robot FROM robots WHERE robot LIKE '%s'",strtolower($user_agent)); as sprintf("SELECT robot FROM robots WHERE robot LIKE '%%s%'",strtolower($user_agent)); blows up in a spectacular ball of flame, PS sorry for the rather basic question. I'm cutting down on my c...

create a my_printf that sends data to both a sprintf and the normal printf?

Hi I am playing with the printf and the idea to write a my_printf(...) that calls the normal printf and a sprintf that sends the result to a special function. (I was thinking about sprintf since that behaves just like printf on most platforms). My idea was to write a small macro that did this: #define my_printf(X, Y...) do{ printf...

Is there any way to determine how many characters will be written by sprintf?

I'm working in C++. I want to write a potentially very long formatted string using sprintf (specifically a secure counted version like _snprintf_s, but the idea is the same). The approximate length is unknown at compile time so I'll have to use some dynamically allocated memory rather than relying on a big static buffer. Is there any ...

In Perl, how can I limit the number of places after the decimal point but have no trailing zeroes?

This question is similar to "dropping trailing ‘.0’ from floats", but for Perl and with a maximum number of digits after the decimal. I'm looking for a way to convert numbers to string format, dropping any redundant '0', including not just right after the decimal. And still with a maximum number of digital, e.g. 3 The input data is fl...

How do display functions like sprintf convert numbers to strings?

I'm trying to extract the integer and decimal parts of a floating point value, and I seem to be running into some strange rounding problems, due probably to the imprecise way floats are stored. I have some code like this to extract the fractional part: double number = 2.01; int frac = int(floor(number * 100)) % 100; However the resu...

Javascript printf/string.format

I'm looking for a good Javascript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .NET). My basic requirement is thousand seperator format for numbers for now, but something that handles lots of combinations (including dates) would be good. I realise Microsoft's AJAX library provides a ...

Customizable implementation of sprintf()

Can anyone point me to a source code file or to a package that has a good, reusable implementation of sprintf() in C which I can customize as per my own need? An explanation on why I need it: Strings are not null terminated in my code (binary compatible). Therefore sprintf("%s") is useless unless I fix the code to understand how to rend...

C: using sprintf and strncpy inserting data into an array of pointers

Hello, I have a structure that has an array of pointers. I would like to insert into the array digits in string format, i.e. "1", "2", etc.. However, is there any difference in using either sprintf or strncpy? Any big mistakes with my code? I know I have to call free, I will do that in another part of my code. Many thanks for any adv...

sprintf outcome problem

Here is my code: <?php $variable1 = 00001; $variable2 = (sprintf('%04d', $variable1 + 1)); echo $variable2; ?> How come the outcome of this code is "0002" and not "00002"? in other words it is missing a 0. ...

How can I sprintf a big number in Perl?

On a Windows 32-bit platform I have to read some numbers that, this was unexpected, can have values as big as 99,999,999,999, but no more. Trying to sprintf("%011d", $myNum) them outputs an overflow: -2147483648. I cannot use the BigInt module because in this case I should deeply change the code. I cannot manage the format as string, ...

Only show decimal point if floating point component is not .00 sprintf/printf

I am pretty formatting a floating point number but want it to appear as an integer if there is no relevant floating point number. I.e. 1.20 -> 1.2x 1.78 -> 1.78x 0.80 -> 0.8x 2.00 -> 2x I can achieve this with a bit of regex but wondering if there is a sprintf-only way of doing this? I am doing it rather lazily in ruby like so: ("...