gpt4 book ai didi

c - fprintf 字符串终止的心理障碍

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:29 25 4
gpt4 key购买 nike

我对这个完全不知所措。我不明白为什么这不起作用。带有 NULL 终止符的简单字符数组 - 除了当我输出它时,它不会终止!

int file_create(const char *path) {
//trying to trap situations where the path starts with /.goutputstream
char path_left_15[16];
strncpy(path_left_15, path, 15);
printf("%d\n", strlen("/.goutputstream")+1);
path_left_15[strlen("/.goutputstream")+1] = '\0';
printf("%d\n", strlen(path_left_15));
printf("path_left_15: %s\n", path_left_15);
//continue on...
}

这是我的输出:

> 16 

> 16

>/.goutputstream\B7<random memory stuff>

我不明白为什么这没有正确终止。我试过使数组更长,但每次都得到相同的结果。我快疯了!

有人看吗?谢谢。

最佳答案

您的数组只有 16 个元素,但您要写入第 17 个元素。所以这是未定义的行为。

关于c - fprintf 字符串终止的心理障碍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115176/

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