逐行读取文件错误
Read line by line of file error
我必须从文件中解析数据。该文件具有以下结构:
text1.txt 1
text2.txt 2
text3.txt 4
文件可以在这里下载我想要得到text1。txt和1。所以我的代码如下:
#include <stdio.h>
void main()
{
FILE *fp= NULL;
int status= -1;
char name[100];
char *filename="test.txt";
fp= ::fopen(filename, "rt");
if (!fp)
{
printf("Error open: "%s"nn", filename);
return;
}
do
{
int number=0;
status= fscanf(fp, "%s %d", name,number);
}
while (status!= EOF);
}
我正在使用c++。我的问题是它在
处崩溃了status= fscanf(fp, "%s %d", name,number);
错误是"访问冲突写入位置0x00000000"。问题是什么?你能帮我吗?
通过地址传递要扫描的参数给fscanf
:
fscanf(fp, "%s %d", name, &number);
编译器可能警告你:
warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
你的代码有未定义的行为,这是你崩溃的根源。
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 如何在文件和行号中抛出错误
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- AMQP-CPP >处理程序中的错误文件描述符
- Xcode OSX上的C++构建失败,出现多个错误文件IO..不可用:在macOS 10.15中引入
- 为什么生成文件返回 g++: 错误: h 文件或目录 make: *** [生成文件:12: 测试] 错误 1?
- 警告:打开错误文件(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.
- Ubuntu C++ 编译器错误: g++: 错误: 文件.cpp: 没有这样的文件或目录
- main.cc:5:30:致命错误:文件夹/file.h:没有这样的文件或目录
- C++ LNK2005"已定义"错误 - 文件引用自身
- C++:模拟餐饮公司计费程序-无法将错误数据输出到错误文件
- 博兰德C++链接器错误c文件包括
- Visual Studio 2015 代码覆盖率错误文件
- 使用system()与Gnuplot进行绘图会导致错误:文件不可读
- 是否存在fseek/ftell可能给出错误文件大小的情况
- g++编译错误.h文件
- c++编译错误:.o文件未找到
- 打开错误文件的错误消息
- Ndk-build输出添加符号错误.文件格式错误