sprintf

Printing out hex values of a char* array in C gives odd values for binary input.

Here's an odd problem that's been stumping me for a bit. The program is written in C89, and it reads a file into a char* array 16 bytes at a time (using fread and a size of sizeof(char)). The file is fopen'd with the "rb" flags. The array is then passed into a function that basically takes the 16 hex values and sticks it into a string, ...

Extracting timings from a multidimensional array and writing to a file in c

Hello, I'm having trouble extracting the timings from a .srt (subtitle) file and writing it to another file called output.srt. When i run the following i get some funky stuff written onto the output file. // where hr=hours,mn=minutes,sc=seconds,ms=mili seconds #include <stdio.h> #define LINES 50 #define CHARAC 80 int main(void){ FI...

Find how many arguments are being used in a printf() statement

Given some unknown input, how do you tell which variables are being substituted into a (s)printf statement? printf("%s %s", "a", "b"); // both used printf("%s", "a", "b"); // only the first one used printf('%1$s %1$s', "a", "b"); // " " printf('%s %1$s', "a", "b"); // " " printf('%1$s %s %1$s', "a", ...

writing formatted data of unknown length to a string (C programming)

The following C function: int sprintf ( char * str, const char * format, ... ); writes formatted data to a string. The size of the array passed as str should be enough to contain the entire formatted string. However, what if the length of the formatted string is not known ahead of time? How can this function (or another function like ...

SIGSEGV, (seemingly) caused by printf

First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here, but I was unable to find this elsewhere (via Google and Stack Overflow). Here's the gist of the error. If I call printf, sprintf or fprintf anywhere within my code, to display a float, I get a SIGSEGV (EXC_BAD_ACCESS) error. Let me give an example...

sprintf(buf, "%.20g", x) // how large should buf be?

I am converting double values to string like this: std::string conv(double x) { char buf[30]; sprintf(buf, "%.20g", x); return buf; } I have hardcoded the buffer size to 30, but am not sure if this is large enough for all cases. How can I find out the maximum buffer size I need? Does the precision get higher (and theref...

Can the input and output strings to sprintf() be the same?

I have used this type of convention many times in my code in the past: strcpy ( cTmpA, "hello" ); sprintf ( cTmpA, "%s world", cTmpA ); Recently I switched my legacy C compiler to Visual Studio 2005, and found I got a garbled string resulting from the above code. It then occurred to me that perhaps the behaviour of sprintf() is not ri...

How to sprintf an unsigned char?

This doesn't work: unsigned char foo; foo = 0x123; sprintf("the unsigned value is:%c",foo); I get this error: cannot convert parameter 2 from 'unsigned char' to 'char' ...

How to check that vsprintf has the correct number of arguments before running.

I'm trying to use vsprintf() to output a formatted string, but I need to validate that I have the correct number of arguments before running it to prevent "Too few arguments" errors. In essence I think what I need is a regex to count the number of type specifiers, but I'm pretty useless when it comes to regex and I couldn't fund it anyw...

sprintf url formatting in javascript

Hey guys, how do i write a function in javascript that can get the current url eg: http://www.blahblah.com/apps/category.php?pg=1&amp;catId=3021 and depending on a user selection choice, appends another parameter to the url like: http://localhost/buyamonline/apps/category.php?pg=1&amp;catId=3021&amp;limit=5 but heres the catch: Eve...

Is it possible to simulate the behaviour of sprintf("%g") using the Rails NumberHelper methods?

sprintf("%g", [float]) allows me to format a floating point number without specifying precision, such that 10.00 is rendered as 10 and 10.01 is rendered as 10.01, and so on. This is neat. In my application I'm rendering numbers using the Rails NumberHelper methods so that I can take advantage of the localization features, but I can't f...

is it possible to reproduce python's string interpolation in ocaml?

In python, one can use printf like formatting with the "%" operator: "i am %d years old" % 99 or "%s is %d years old" % ("bob", 101) Is there a way to get the same concise syntax in Ocaml, for arbitrary numbers of arguments? For a single argument, the following works: let (%) = Printf.sprintf in ... "i am %d years old" % 99 Is ...

What is the sprintf() pattern to output floats without ending zeros?

I want to output my floats without the ending zeros. Example: float 3.570000 should be outputted as 3.57 and float 3.00000 should be outputted as 3.0 (so here would be the exception!) ...

Can I measure the necessary buffer for sprintf in Microsoft C++?

I'm writing a small proof-of-concept console program with Visual Studio 2008 and I wanted it to output colored text for readability. For ease of coding I also wanted to make a quick printf-replacement, something where I could write like this: MyPrintf(L"Some text \1[bright red]goes here\1[default]. %d", 21); This will be useful becaus...

Problem with sprintf function, last parameters are wrong when written

So I use sprintf sprintf(buffer, "%f|%f|%f|%f|%f|%f|%d|%f|%d", x, y, z, u, v, w, nID, dDistance, nConfig); But when I print the buffer I get the 2 last parameters wrong, they are lets suppose to be 35.0000 and 0 and in the string they are 0.00000 and 10332430 and my buffer is long enough and all the other parameters are good in the st...

Patching an EXE using IDA

Say there is a buggy program that contains a sprintf() and i want to change it to a snprintf so it doesn't have a buffer overflow.. how do I do that in IDA?? ...

Sprintf for Actionscript 3?

Something like sprintf3 for Actionscript 3? I've googled around, and not found much. ...

Is there a free implementation of printf for .net?

The problems: I can't use string.Format, I have C style format strings; I can't call the native printf (no P/Invoke); I can't use http://www.codeproject.com/KB/printing/PrintfImplementationinCS.aspx because of the license, I need something GPL-compatible. Is there a free implementation of printf/sprintf for the .net framework? Other ...

C sprintf causing a segmentation fault.

Hello, I am trying to pass in arguments to the a parent file that is supposed to create a child process for each pair of arguments. The child processes will add up each pair and return their sum to the parent process. If there is an odd number of arguments passed in, I add a 0 to the end of the argv array to make it even. This keeps hap...

How do I use sprintf to zero fill to a variable length in Perl?

I want to use Perl's sprintf to zerofill a variable. sprintf("%08d", $var); but I want the dynamically determine how many digits to zero fill. How do I replace the "8" in sprintf("%08d", $var) with a variable called $zerofill. Thanks. ...