C++处理文本文件中的分段故障11
Segmentation Fault 11 in C++ processing text file
我正试图在Mac中用C++编写一个程序来处理一个包含以下数据的文本文件(table.txt):
汤姆50 60 70.5
杰瑞80.3 65 91
标记75.2 77 92.7
Lucy 100 87.6 93
然而,我从终端上运行它得到的是,分段故障11:
汤姆50 60 70.5
杰瑞80.3 65 91
标记75.2 77 92.7
分段故障:11
这是我的程序:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct StudentList {
string name;
double scores[2];
};
int main() {
ifstream marks;
marks.open("table.txt");
StudentList Student[50];
int index = 0;
string text;
if (marks.fail()) {
cout << "fail open" << endl;
}
while (marks >> text) {
cout << text << " ";
Student[index].name = text;
marks >> Student[index].scores[0];
cout << Student[index].scores[0] << " ";
marks >> Student[index].scores[1];
cout << Student[index].scores[1] << " ";
marks >> Student[index].scores[2];
cout << Student[index].scores[2] << " ";
cout << endl;
index++;
cout << index << endl;
}
marks.close();
return 0;
}
到底是什么问题?
在C中,与大多数现代编程语言一样,数组索引从0开始,减速中的数字是大小,而不是最后一个索引。所以
double scores[2];
声明一个大小为2的数组,索引为0和1。
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段故障 运行C++代码时出现 SIGSEGV
- 分段故障背包问题
- 分段故障 11,从类函数显示动态 C 字符串
- 面临分段故障 使用 ffmpeg 读取视频时,因为"pFormatCtx-> streams [i]-> codecpar"的地址0x00
- 在C++中,当指向删除和指向不同对象时,分段故障指针
- 分段故障说明
- 分段故障(核心转储)-不知道为什么
- 分段故障线程
- hiredis SET遇到分段故障
- 分段故障,合并排序算法
- 多线程程序中的分段故障和gdb回溯上的不完整信息
- 到达主C++之前分段故障
- 分段故障核心使用 IF流转储
- 使用向量的移动键盘排列(分段故障)
- 在二进制树插入和遍历期间,我得到了分段故障
- 分段故障在类之间返回整数
- C++分段故障BST