snprintf

Are snprintf and friends safe to use?

There was a question recently on SO (Why on earth would anyone use strncpy instead of strcpy?), which hade answers (answer 1, answer 2), that made me uncertain about other string functions with 'n' in their name, like snprintf (which I have been using extensively). Is snprintf safe to use? And generally, what are the safe functions from ...

Help gcc to not warn about not using a string literal format string

I'm creating a function in C to convert an index value into a string, which is a verbose description of the "field" represented by the index. So, I have a nice array with all the verbose descriptions indexed by, well the index. To dump it into a buffer I use code like this #define BUFFER_SIZE 40 void format_verbose(uint32_t my_index, ...

Resuming [vf]?nprintf after reaching the limit

I have an application which prints strings to a buffer using snprintf and vsnprintf. Currently, if it detects an overflow, it appends a > to the end of the string as a sign that the string was chopped and prints a warning to stderr. I'm trying to find a way to have it resume the string [from where it left off] in another buffer. If this...

using snprintf to avoid buffer overruns

Hello, gcc 4.4.1 C99 I am using snprintf like this to avoid a buffer overrun. char err_msg[32] = {0}; snprintf(err_msg, sizeof(err_msg) - 1, "[ ST_ENGINE_FAILED ]"); I have added the minus 1 incase the string is over 32 bytes long and I want to include the null terminator. Am I correct in my thinking? Many thanks for any suggestio...

snprintf vs strcpy(etc) in c

For doing string concatenation I've been doing basic strcpy,strncpy of char* buffers. Then I learned about the snprintf and friends. Should I stick with my strcpy,strcpy + \0 terminiation. Or should I just use snprintf in the future? thanks ...

casting doubles to integers in order to gain speed

Hello all, in Redis (http://code.google.com/p/redis) there are scores associated to elements, in order to take this elements sorted. This scores are doubles, even if many users actually sort by integers (for instance unix times). When the database is saved we need to write this doubles ok disk. This is what is used currently: snprin...

Is a strlen call in snprintf causing this segfault?

I have a void *, call if data which length I know, but is not terminated. I make a call like this snprintf(line, sizeof(line), "%*s", n, (const char*)data) where n is the known length. Almost always this works, but occasionally it results in a segfault. Whenever the segfault occurs, the back trace says the problem is inside strlen. ...

Why do I get segfaults when declaring a struct globally or extern?

I have a struct defined in a header as follows: #define LC_ERR_LEN 300 typedef struct dLC_ERRMSG { short nr; short strategy; char tx[LC_ERR_LEN]; } LC_ERRMSG; Which I use in my code as such: LC_ERRMSG err; char *szError; szError = strerror(sStatus); snprintf(err.tx,LC_ERR_LEN," %s - %s",szFilename,szError); /* do something w...

What is C# analog of C snprintf()?

What is C# analog of C snprintf()? In C code here we use snprintf(buf, sizeof(buf), outfilename, frame); What could be its exact analog? ...

snprintf : simple way to force . as radix?

My program was not behaving correctly on one machine so I started to hunt for the bug, and I discovered that on that machine, snprintf uses a comma (,), not a . (dot) as 99% of other computers (at least in my experience). Shouldn't this be standardized? I am using a library that assumes that the radix is a . (dot) and so it does not wo...

snprintf overflows and prints garbage to file randomly. help

Here is my code, basically one the 4 computer I have tested it on they all work perfectly with very large data sizes, eg textfiles up to 500mb in size, but when I run them on the server with real data even files as small as 6mb seem to overrun somewhere and writes garbage to the end of my files. Here is the source of the entire function...