gpt4 book ai didi

将 header 与缓冲区内容连接起来

转载 作者:行者123 更新时间:2023-11-30 17:42:44 24 4
gpt4 key购买 nike

我正在逐 block 读取文件并通过 TCP 连接发送该文件。对于每个 block ,我必须在发送消息之前添加一个短 header 。对于我的一生,我无法让它发挥作用:

do {
bytes_read = fread( &buffer, sizeof(char), BUFFSIZE-sizeof("your_file||")-1, fp );
bzero(message, BUFFSIZE);
strcpy(message, "your_file|");

for (j=0; j<bytes_read; ++j) {
fprintf(stdout, "%c", buffer[j]);
message[j+9] = buffer[j];
}

strcat(message, "|");
strcat(message, "\0");
fprintf(stdout, "message: %s\n\n", message);
} while (!feof(fp1));

我的 printf 输出显示了完美的数据,所以我知道我正在正确读取。但我无法收到此消息。这就是我的输出:

BEGIN_LINE000This is a test file with exactly 10 lines of readable textEND_LINE000
message: your_file|

BEGIN_LINE001This is a test file with exactly 10 lines of readable textEND_LINE001
BEGIN_LINE002This is a test file with exactly 10 lines of readable textEND_LINE002
BEGIN_LINE003This is a test file with exactly 10 lines of readable textEND_LINE003
BEGIN_LINE004This is a test file with exactly 10 lines of readable textEND_LINE004
BEGIN_LINE005This is a test file with exactly 10 lines of readable textEND_LINE005
BEGIN_LINE006This is a test file with exactly 10 lines of readable textEND_LINE006
BEGIN_LINE007This is a test file with exactly 10 lines of readable textEND_LINE007
BEGIN_LINE008This is a test file with exactly 10 lines of readable textEND_LINE008
BEGIN_LINE009This is a test file with exactly 10 lines of readable textEND_LImessage: your_file|

NE009
BEGIN_LINE010This is a test file with exactly 10 lines of readable textEND_LINE010message: your_fileNE009
BEGIN_LINE010This is a test file with exactly 10 lines of readable textEND_LINE010|

我确信我忽略了一些非常简单的事情,但我花了几个小时尝试不同的方法来解决这个问题,但我总是回到同样的问题。谁能告诉我我做错了什么?谢谢。

最佳答案

不能保证 for 循环最终会在 message 中留下正确终止的字符串,除非您知道 buffer 的最后一个字符code> 始终为 '\0'

这种终止失败使得在 message 上调用 strcat() 以及(当然)将其打印为字符串变得危险。

关于将 header 与缓冲区内容连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20426688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com