C语言 将字符串添加到 C 中的字符数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3557780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:18:02  来源:igfitidea点击:

Add string to char array in C

carraysstringcharfgets

提问by skylerl

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

我有一个名为 buf 的 C 数组。这是它的定义:

char buf[1024];

char buf[1024];

Now, my current code takes from stdinand 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:

现在,我当前的代码从stdin并用于fgets()设置该数组,但是我希望使用代码来设置它。现在设置 buf 的行如下所示:

fgets(buf, 1024, stdin);

fgets(buf, 1024, stdin);

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

基本上,我想用说...“我的字符串”来替换标准输入。做到这一点的最佳方法是什么?

回答by Cedric H.

Look for sprintf, for example here: Cpp reference

寻找sprintf,例如在这里:Cpp 参考

Edit:

编辑:

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

Edit 2:

编辑2:

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

根据建议避免溢出(但这是标准 C),您可以使用snprintf

回答by user411313

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

snprintf 只是 C99 不是 C89,sprintf_s/strcpy_s 只是 MSVC,不是 C89,不是 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.

它适用于所有 ANSI C 环境。

回答by James Curran

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.

Microsoft 的编译器还包含一个函数 strcpy_s,它是 strcpy 的“安全”版本。它确保您不会超支buf。在这种特殊情况下,这可能不是问题,但您应该知道。但是,请注意,它不适用于任何其他编译器,因此不能用于需要可移植性的地方。

 strcpy_s(buf, sizeof(buf), "My String");

回答by rubber boots

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 todo in your code.

他们中的大多数人不确定/不安全。究竟应该采取什么取决于您真正想在代码中做什么

Regards

问候

rbo

红包

回答by atlpeg

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 );