gpt4 book ai didi

c - 如何向字符添加字符

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

我正在做:

FILE *in;
extern FILE *popen();
char buff[512];

if (!(in = popen("df / | tail -n +2 | awk '{ print $1 }'", "r"))) {
exit(1);
}

while (fgets(buff, sizeof(buff), in) != NULL ) {
printf("Output: %s", buff);
}

因此,一旦我有了 buff,如何在末尾附加额外的字符,例如 s0 ,以便我可以将此 char 传递给可以使用它的函数吗?

最佳答案

像这样的东西可以解决问题:

char buff[512];
size_t pos = 0;
...
while (fgets(buff + pos, sizeof(buff) - pos, in) != NULL) {
printf("Output: %s\n", buff + pos);
pos += strlen(buff + pos);
}

如果您想添加不是来自文件的字符,您可以扩展这个想法:

strcpy(buff + pos, "s0");
pos += strlen(buff + pos);

buf[pos++] = 's';
buf[pos++] = '0';
buf[pos++] = '\0';

您必须确保缓冲区不会溢出。

关于c - 如何向字符添加字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15687226/

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