gpt4 book ai didi

c++ - C++ 中的 FILE I/O 抛出错误

转载 作者:行者123 更新时间:2023-11-28 00:16:14 25 4
gpt4 key购买 nike

int main(array<System::String ^> ^args)
{

FILE* fp;
char str[1];
int x = 0;

fp = fopen("C:\\input.txt", "r");

do{
x = fscanf(fp, "%s", str);

if (x != -1) // So that the last string is not printed twice
{
printf("%s ", str);
}

}while (x != -1);

return 0;

}

这个程序打印出正确的输出。但是在打印之后,它会在窗口中抛出一个错误,提示“程序停止工作”。此外,如果记事本文件中没有文本,即如果它是空白的,那么它不会显示错误。请解释!

最佳答案

问题在于您有一个单字节的 char 数组,您正在将字符串读入其中。您需要使用一个足够大的数组来容纳最大的数据。

关于c++ - C++ 中的 FILE I/O 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30132551/

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