gpt4 book ai didi

c++ - 将文件读入 char 数组,然后计算 strlen 卡在 C++ 中

转载 作者:行者123 更新时间:2023-11-30 04:29:52 25 4
gpt4 key购买 nike

我有以下C程序

f = fopen("input", "r");
char line[1024];
while ((fgets(line, 1024, f)) != NULL) {
cout << strlen(line) << endl;
exit(1);//just to see what went wrong.
}
fclose(f);

看起来它在评估strlen(line) 时会卡住。但这对我来说似乎很奇怪,因为我检查了行中的各个字符,并且发现了 line[3]=='\0'。那为什么会不停呢?

最佳答案

您的代码绝对应该有效。问题不在您粘贴的代码中。可能的问题来源是:

1) 你有合适的“#include”指令吗?

2) 您的编译器、库、环境或系统是否损坏?

3) 您是否将此代码与其他以某种方式破坏环境的代码结合在一起运行?

4) 您是否处在不正常的环境中? (例如,堆栈非常有限的微 Controller 。)

关于c++ - 将文件读入 char 数组,然后计算 strlen 卡在 C++ 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9056232/

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