views:

103

answers:

5

I have a C array called buf. Here is it's definition:

char buf[1024];

Now, my current code takes from stdin and uses fgets() to set that array, however I wish to use code to set it instead. Right now the line that sets buf looks like this:

fgets(buf, 1024, stdin);

Basically, I want to replace stdin, with say... "My String". What's the best way to do this?

+2  A: 

Look for sprintf, for example here: Cpp reference

Edit:

sprintf(buf, "My string with args %d", (long) my_double_variable);

Edit 2:

As suggested to avoid overflow (but this one is standard C) you can use snprintf.

Cedric H.
look at sprintf_s to make it safe and avoid buffer overflow attacks.
KLee1
@KLee1: Generally good advice. However, given that the string is going to be supplied inside the code, not a user supplied buffer, a buffer overflow attack seems unlikely.
torak
Yep that did it! Thanks, will be using _s as well. Thanks guys!
Yakattak
@KLee1: I saw nothing in the question to indicate the OP is working in an environment that has "sprintf_s", which does not appear to be in C99.
David Thornley
+1 for standards and portability, avoiding the `_s` functions.
Stephen P
Nver use sprintf_s. Its not portable.
mathepic
All the _s functions are Microsoft extensions, and almost all of them are pointless duplicates of functionality that was available somewhere else. sprintf_s is no better than snprintf. But definitely use snprintf.
Zack
@all Sorry, I meant snprintf. My bad.
KLee1
+2  A: 
strcpy(buf, "My String");

Microsoft's compiler also include a function strcpy_s, which is a "safe" version of strcpy. It makes sure that you won't overrun buf. In this particular case, that's probably not a problem, but you shoul dknow about. But, be aware, it's not available with any other compiler, so it can;t be used where portable is needed.

 strcpy_s(buf, sizeof(buf), "My String");
James Curran
+1  A: 

There are many variants, some have been already proposed, some not:

...

char input[] = "My String";

strcpy(buf, input);

strncpy(buf, input, sizeof(buf));

sscanf(input, "%s", buf);

sprintf(buf, "%s", input);

memcpy(buf, input, strlen(input));

...

most of them are unsure/insecure. What exactly should be taken depends on what you really want to do in your code.

Regards

rbo

rubber boots
A: 

Verbose but safe:

int copy_size = sizeof( buf ) - 1 < strlen( MyString ) ? sizeof( buf ) - 1 : strlen( MyString );
memset( buf, 0, copy_size + 1 );
memcpy( buf, MyString, copy_size );
atlpeg
this is horrible. why do you zero out so much memory just to overwrite it with memcpy? look at BSD's strlcpy.
+2  A: 

snprintf is only C99 not C89, sprintf_s/strcpy_s are only MSVC, not C89, not C99.

char *mystr="come from stdin or file or ...";
char buf[1024];
...
memset(buf,0,sizeof buf);
strncpy(buf,mystr,(sizeof buf)-1);

or non array:

#define BUFLEN 512
char *mystr="come from stdin or file or ...";
char *buf;
...
char *buf=calloc(1,BUFLEN);
strncpy(buf,mystr,BUFLEN-1);

It works on all ANSI C environments.

+1 for being C89 compatible.
mathepic