gpt4 book ai didi

C 在 while 循环中连接字符串

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

我似乎无法使用以下函数在 while 循环中连接字符串。知道我做错了什么吗?

void do_file(FILE *in, FILE *out, OPTIONS *options)
{
char ch;
int loop = 0;
int sz1,sz2,sz3;

int seeker = offsetof(struct myStruct, contents.datas);

//find total length of file
fseek(in, 0L, SEEK_END);
sz1 = ftell(in);

//find length to struct beginning and minus that from total length
fseek(in, seeker, SEEK_SET);
sz2 = sz1 - ftell(in);

sz3 = (sz2 + 1) * 2;//Allocate enough size for 2x array length
char buffer[sz3];//set size of buffer to be copied to
char msg[sz3];//set size of msg

buffer[0] = '\0';

while (loop < sz2)
{
if (loop == sz2)
{
break;
}

fread(&ch, 1, 1, in);
//sprintf(msg, "%02X", (ch & 0x00FF));
strcat(buffer, msg);

++loop;
}
printf("%s\n", buffer);
}

最佳答案

您唯一的 strcat 附加了从未填充的 char msg[sz3];

编辑最简单的修复方法是添加

msg[1] = '\0';

之后

char msg[sz3];//set size of msg

并替换

fread(&ch, 1, 1, in);

fread(&msg[0], 1, 1, in);

关于C 在 while 循环中连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39463758/

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