C - 如何将字符串数组连接到缓冲区?

时间:2022-01-08 19:36:10

I am trying to concatenate a random number of lines from the song twinkle twinkle. Into the buffer before sending it out because I need to count the size of the buffer.

我试图连接歌曲闪烁闪烁的随机数行。在发送之前进入缓冲区因为我需要计算缓冲区的大小。

My code:

我的代码:

char temp_buffer[10000];
char lyrics_buffer[10000];
char *twinkle[20];
int arr_num;
int i;

twinkle[0] = "Twinkle, twinkle, little star,";
twinkle[1] = "How I wonder what you are!";
twinkle[2] = "Up above the world so high,";
twinkle[3] = "Like a diamond in the sky.";
twinkle[4] = "When the blazing sun is gone,";
twinkle[5] = "When he nothing shines upon,";

srand(time(NULL));
arr_num = rand() % 5;

for (i=0; i<arr_num; i++);
{
    sprintf(temp_buffer, "%s\n", twinkle[i]);
    strcat(lyrics_buffer, temp_buffer);
}

printf("%s%d\n", lyrics_buffer, arr_num);

My current code only prints 1 line even when I get a number greater than 0.

我的当前代码只打印1行,即使我得到一个大于0的数字。

2 个解决方案

#1


2  

There are two problems: The first was found by BLUEPIXY and it's that your loop never does what you think it does. You would have found this out very easily if you just used a debugger to step through the code (please do that first in the future).

有两个问题:第一个是由BLUEPIXY发现的,而且你的循环永远不会做你想象的那样。如果您只是使用调试器来逐步完成代码,那么您可以非常轻松地找到它(请在将来首先执行此操作)。

The second problem is that contents of non-static local variables (like your lyrics_buffer is indeterminate. Using such variables without initialization leads to undefined behavior. The reason this happens is because the strcat function looks for the end of the destination string, and it does that by looking for the terminating '\0' character. _If the contents of the destination string is indeterminate it will seem random, and the terminator may not be anywhere in the array.

第二个问题是非静态局部变量的内容(比如你的lyrics_buffer是不确定的。在没有初始化的情况下使用这些变量会导致未定义的行为。发生这种情况的原因是因为strcat函数查找目标字符串的结尾,它确实通过查找终止'\ 0'字符。如果目标字符串的内容是不确定的,它将看起来是随机的,并且终结符可能不在数组中的任何位置。

To initialize the array you simply do e.g.

要初始化阵列,您只需执行以下操作:

char lyrics_buffer[10000] = { 0 };

That will make the compiler initialize it all to zero, which is what '\0' is.

这将使编译器将其全部初始化为零,这就是'\ 0'。

This initialization is not needed for temp_buffer because sprintf unconditionally starts to write at the first location, it doesn't examine the content in any way. It does, in other words, initialize the buffer.

temp_buffer不需要这个初始化,因为sprintf无条件地开始在第一个位置写入,它不会以任何方式检查内容。换句话说,它确实初始化缓冲区。

#2


0  

Update the buffer address after each print after initializing buffer with 0.

在使用0初始化缓冲区后,在每次打印后更新缓冲区地址。

char temp_buffer[10000] = {0};
for (i=0; i<arr_num; i++) //removed semicolon from here
{
    sprintf(temp_buffer + strlen(temp_buffer), "%s\n", twinkle[i]);
}

temp_buffer should contain final output. Make sure you have enough buffer size

temp_buffer应包含最终输出。确保您有足够的缓冲区大小

You don't need strcat

你不需要strcat

#1


2  

There are two problems: The first was found by BLUEPIXY and it's that your loop never does what you think it does. You would have found this out very easily if you just used a debugger to step through the code (please do that first in the future).

有两个问题:第一个是由BLUEPIXY发现的,而且你的循环永远不会做你想象的那样。如果您只是使用调试器来逐步完成代码,那么您可以非常轻松地找到它(请在将来首先执行此操作)。

The second problem is that contents of non-static local variables (like your lyrics_buffer is indeterminate. Using such variables without initialization leads to undefined behavior. The reason this happens is because the strcat function looks for the end of the destination string, and it does that by looking for the terminating '\0' character. _If the contents of the destination string is indeterminate it will seem random, and the terminator may not be anywhere in the array.

第二个问题是非静态局部变量的内容(比如你的lyrics_buffer是不确定的。在没有初始化的情况下使用这些变量会导致未定义的行为。发生这种情况的原因是因为strcat函数查找目标字符串的结尾,它确实通过查找终止'\ 0'字符。如果目标字符串的内容是不确定的,它将看起来是随机的,并且终结符可能不在数组中的任何位置。

To initialize the array you simply do e.g.

要初始化阵列,您只需执行以下操作:

char lyrics_buffer[10000] = { 0 };

That will make the compiler initialize it all to zero, which is what '\0' is.

这将使编译器将其全部初始化为零,这就是'\ 0'。

This initialization is not needed for temp_buffer because sprintf unconditionally starts to write at the first location, it doesn't examine the content in any way. It does, in other words, initialize the buffer.

temp_buffer不需要这个初始化,因为sprintf无条件地开始在第一个位置写入,它不会以任何方式检查内容。换句话说,它确实初始化缓冲区。

#2


0  

Update the buffer address after each print after initializing buffer with 0.

在使用0初始化缓冲区后,在每次打印后更新缓冲区地址。

char temp_buffer[10000] = {0};
for (i=0; i<arr_num; i++) //removed semicolon from here
{
    sprintf(temp_buffer + strlen(temp_buffer), "%s\n", twinkle[i]);
}

temp_buffer should contain final output. Make sure you have enough buffer size

temp_buffer应包含最终输出。确保您有足够的缓冲区大小

You don't need strcat

你不需要strcat