我写了一段代码,从名为 network.dat 的文件中读取
我写的代码是
f = fopen("network.dat", "r");
if(f == NULL)
exit(1);
int read, N;
printf("%p\n", f);//output file pointer, included this just to check if file is opened properly
fscanf(f, "%d%d", &N, &read);//error here
cout<<N;
文件被正确打开并得到文件指针 (49897488
) 作为输出,但它后面的行是程序停止工作的地方,我没有得到 N
作为输出。
请告知是否需要其他详细信息。
network.dat 的内容是
10 1
1 6 1.28646
1 7 1.2585
2 9 1.33856
等等。我只关注文件中的前 2 个数字,即 10 和 1。
最佳答案
您的 scanf() 格式字符串不正确。 "%d,%d"查找两个用逗号分隔的整数。如果您想读取两个由空格分隔的整数,只需执行“%d%d”即可。
关于c++ - 无法从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12239122/