String concatenation in C - c

if I want to construct a const char * out of several primitive type arguments, is there a way to build the string using a similar to the printf?

You're probably looking for snprintf.
int snprintf(char *str, size_t size, const char *format, ...);
A simple example:
char buffer[100];
int value = 42;
int nchars = snprintf(buffer, 100, "The answer is %d", value);
printf("%s\n", buffer);
/* outputs: The answer is 42 */
GNU has an example too.
Just to add, you don't actually need to use snprintf - you can use the plain old sprintf (without the size argument) but then it is more difficult to ensure only n characters are written to the buffer. GNU also has a nice function, asprintf which will allocate the buffer for you.

You can use sprintf, which is exactly like printf except the first parameter is a buffer where the string will be placed.
Example:
char buffer[256];
sprintf(buffer, "Hello, %s!\n", "Beta");

Related

Defining a string as in fprintf syntax

How to build up a string in C that includes parameters? Is there a way similar to fprintf syntax to do it? For example here's a little testProg.c:
int main(int argc, char *argv[]) {
printf("You are running %s program.\n", argv[0]);
// It will print: "You are running testProg.c program.\n"
char myString[];
// I want the string printed by printf to be saved inside myString[]
// ...
}
You can use snprintf. First determine the buffer size for myString that will fit the content. Then use snprintf to fill in the content.
char myString[100] = "";
snprintf( myString, sizeof(myString), "You are running %s program.", argv[0] );
snprintf is similar to fprintf (which is the general version of printf with the output stream being stdout), so all control formats applied - details can be found in the standard:
7.21.6.5 The snprintf function
#include <stdio.h>
int snprintf(char * restrict s, size_t n,
const char * restrict format, ...);

assigning an int into a string in C

I need to call a function which receives a string.
I need the string to contain integers.
How do I assign the string with the integers?
Thanks,
Edo Cohen
You are looking for the snprintf() function:
http://libslack.org/manpages/snprintf.3.html
Use snprintf to make the string you need.
try this:
int number = 10;
char mystring[20];
snprintf(mystring, sizeof(mystring), "%d", number);
alternatively you can use itoa but this is considered unsafe.
use itoa, or sprintf as suggested in the link, because of portability reasons.
snprintf or asprintf for security reasons..
You could use sprintf to do that:
sprintf(buffer, "%d", 123);
Note that you need to mage sure that your buffer has enough space allocated
i guess this will work
#include <stdlib.h> // for itoa() call
#include <stdio.h> // for printf() call
int main() {
int num = 123;
char buf[5];
// convert 123 to string [buf]
itoa(num, buf, 10);
// print our string
printf("%s\n", buf);
return 0;
}
also u can use--
#include <stdio.h>
int main(void)
{
const char base[] = "filename";
char filename [ FILENAME_MAX ];
int number = 42;
sprintf(filename, "%s%d", base, number);
printf("filename = \"%s\"\n", filename);
return 0;
}
Though remember sprintf() does not calculate for buffer overflow. sprintf() doesn't flush any file buffer, in fact it doesn't have any buffer associated with it. sprintf() operates on a character buffer (not quite the same thing). If you overflow that buffer you can crash the system.
A safer version of sprintf() is snprintf() which unfortunately isn't very standard yet. It appears in various forms from many compiler vendors and was finally standardized in C99 and will hopefully make it as part of standard C++ in the next standard of C++. Until then you just have to dig up the unstandard version of it if you have one.

Is there a function akin to fprintf but only returns the result of formated string in C?

fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
I don't want to output anything via fprintf,but only the result of "Error in pcap_findalldevs: %s\n", errbuf,what's the function for that?
snprintf allows you to format to a char buffer and performs bounds checking to ensure the buffer is not overrun.
The commonly used sprintf does not perform bounds checking, and as such is inherently unsafe.
int sprintf(char * ptr, const char * format, ...)
Writes the output and a terminating null to a buffer at ptr, returns the number of characters written excluding the null. Dangerous if you don't know how big your output should be, it will blindly write past the end of the buffer.
int snprintf(char * ptr, size_t n, const char * format, ...)
Same as sprintf, but will write a maximum of n characters, including the trailing null. Returns the number of characters that would be written if n was large enough, so that you can reallocate your buffer if necessary.
int asprintf(char ** ptr, const char * format, ...)
Same as sprintf except a double pointer is passed in, the buffer will be resized if necessary to fit the output. This is a GNU extension, also available in BSD, it can be emulated like (Ignoring error checking)
int asprintf(char ** ptr, const char * format, ...){
va_list vlist;
va_start(vlist,format);
int n = vsnprintf(NULL,0,format,vlist);
*ptr = realloc(*ptr,n+1);
n = vsnprintf(*ptr,n+1,format,vlist);
va_end(vlist);
return n;
}
That is sprintf() which also has some useful variations, like vsprintf() which takes a pointer to an argument list. There are also buffer protection versions in some implementations.
sprintf copies the result to a char * instead of writing it to stdout or a file.
Syntax differences
printf(const char format, ...)
fprintf(FILE * file, const char format, ...)
sprintf(char * string, const char format, ...)
sprintf is the original call, but should be considered deprecated in favor of snprintf which allows you to pass a size. Another alternative is asprintf, which will allocate a string large enough to hold the result.

Can you write to a byte array using a FILE*

C# has a neat feature of being able to write to a memory stream using the MemoryStream object.
I'm looking for similar functionality from C using a FILE* pointer.
I want to be able to sprintf() but with the added functionality of having C remember "where I was" in the buffer I'm writing to.
If you are using the GNU C Library, you can use fmemopen(). There may be other non-portable extensions for other environments, but there's no portable way using FILE*s.
You could also, however, wrap snprintf, if you don't insist on actually using FILE*s. For example, glib (note: not the same as the GNU C Library, and portable) has a g_string_append_printf that does what you want.
There's also an ugly hack which works with plain ISO-C: You can use fopen() to open a null file (/dev/null on *nix, NUL on Windows) and set the array as the file's buffer via
setvbuf(file, buffer, _IOFBF, buffer_size)
This should work fine as long as fflush() isn't called anywhere in the code. Also, the programmer has to explicitly take care of the string delimiter.
I don't really see a need to do this, though: As snprintf() returns the number of characters written, it's trivial to keep track of a buffer's position.
One can even write a function to automagically resize the buffer on overflow: bufprintf.c
The function's prototype is
int bufprintf(char **buffer, size_t *size, size_t *offset,
const char *format, ...);
An example program could look like this:
#include <stdio.h>
extern int bufprintf(char **buffer, size_t *size, size_t *offset,
const char *format, ...);
int main(void)
{
size_t size = 0; // must be set!
size_t offset;
char * buffer;
for(int i = 0; i < 100; ++i)
bufprintf(&buffer, &size, &offset, "we rock %i\n", i);
puts(buffer);
printf("size:\t%u\noffset:\t%u\n", (unsigned)size, (unsigned)offset);
}
sprintf returns the number of characters that were printed into the string. You can use that value to increment the pointer of your buffer.
buffer += sprintf(buffer, "%d", i);
Make sure that you keep around a copy of the original pointer, as that is what you will be using when passing the buffer somewhere else.
Why not use mmap? You can map a file to memory and use a FILE* pointer.
Update: It does not work. Sorry.
MemoryStream is not a feature of C#, the language. It's simply a class in the BCL. You could write it yourself in C#.
And so it is in C - you'd write some functions that work similarly in use to fopen, fprintf, fwrite, fclose, etc., but give them names like mopen, mwrite, etc. (presumably) and write them so they operate on a memory buffer.
How about just manually managing your pointers. I don't have a compiler in front me but hopefully the code below gets the idea across:
char huge_buffer[REALLY_BIG_SIZE];
char *write_pos = huge_buffer;
//...
void fprintf_to_mem(char **mem_ptr, const char *fmt, ...)
{
va_list args;
int num_written;
va_start(args, mem_ptr);
num_written = vsprintf(*write_pos, fmt, args);
*write_pos += num_written;
va_end(args);
}
//...
fprintf_to_mem(&write_pos, "Testing %d %d %d", 1, 2, 3);
fprintf_to_mem(&write_pos, "Hello world!\r\n");
I would expect that to output "Testing 1 2 3Hello world!\r\n" to huge_buffer.

Append formatted string to string in C without pointer arithmetic

I wrote a very simple C function to illustrate what I would like to simplify:
void main(int argc, char *argv[])
{
char *me = "Foo";
char *you = "Bar";
char us[100];
memset(us, 100, 0x00);
sprintf(us, "You: %s\n", you);
sprintf(us + strlen(us), "Me: %s\n", me);
sprintf(us + strlen(us), "We are %s and %s!\n", me, you);
printf(us);
}
Is there a standard library function to handle what I'm doing with sprintf and advancing the pointer?
sprintf returns the number of non-NUL characters written.
int len = 0;
len += sprintf(us+len, ...);
len += sprintf(us+len, ...);
...
char *me="Foo";
char *you="Bar";
char us[100];
char* out = us;
out += sprintf(out,"You: %s\n",you);
out += sprintf(out,"Me: %s\n",me);
out += sprintf(out,"We are %s and %s!\n",me,you);
printf("%s", us);
You could just use something like this:
sprintf(us, "You: %s\nMe: %s\nWe are %s and %s!\n", you, me, me, you);
You are probably looking for strcat (or even better strncat).
Never use a non-constant string as the first argument to printf (or second argument to fprintf. This is bad practice. Try changing the me variable to "Muahaha %n%d%n%d%n%d%n%d%n" and say goodbye to your stack and say hello to SEGFAULT. printf will try and format the string but you haven't provided any arguments for it to deal with, and even though there are format specifiers in the string printf has no idea that you haven't given it any arguments.
If you want to output a 'pre-formatted' string, use fputs(str,stdout). There is rarely a case to use printf with a non-constant format string.

Resources