读取结构数组时出错
Out of Range Error When Reading to Struct Array
所以我正在为数据库存储编写一个程序,第一步是将信息从文本文件加载到结构数组中。但是,我在读取/写入过程中收到一条错误消息,指出程序正在进入超出范围的实例。
while (!inFile.eof())
{
getline(inFile, dataLine); //saves the line of the file into a string
a[i].name = dataLine.substr(0, 17); // 18
a[i].author = dataLine.substr(19, 33); // 15
a[i].vol = dataLine.substr(35, 60); // 26
a[i].pub = dataLine.substr(62, 77); // 16
a[i].year = dataLine.substr(79, 82); // 4
a[i].price = dataLine.substr(84, 91); // 8
a[i].copies = dataLine.substr(93, 96); // 3
i++; //moves through the array after each line.
count++; //counts how many lines/items there are in the file entered for the program
}
我已经将问题缩小到本节,但我似乎无法弄清楚导致它出错的原因。
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 19) > this->size() (which is 0)
Aborted
这是我收到的错误消息。
您面临的特定错误是dataLine
的长度为零,并且您正在尝试子字符串。此处描述了异常:http://www.cplusplus.com/reference/string/string/substr/
对字符串的长度进行额外检查将解决此问题。
if (dataLine.size() >= 97) {
...
}
相关文章:
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 将链表转换为指针数组时出错
- 为 NewObjectA() 函数创建 jvalues 的参数数组时出错 - JNI Invocation API
- 基于数组的列表 - 动态数组创建时出错
- 从斐波那契序列 c++ 中的数组中查找正确的元素时出错
- 分段 使用 MPI_Gather 收集字符数组时出错
- 读取字符串字符时出错 - 对于数组中的字符串值
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 在 c++ 中返回一维数组时出错
- 将 2D 数组传递给 C++ 中的函数时出错
- 在 c++ 上创建动态数组时出错
- 尝试返回指向 Visual Studio 中的动态数组的指针时出错C++
- 将 find_if() 与数组一起使用的代码段中出错
- 在比较特定输入中的程序的数组中出错
- 分段 填充数组时出错
- 数组作为函数参数查找数组长度时出错
- 读取结构数组时出错
- 将数组指针传递到函数时出错
- C++将指针数组传递给函数指针数组时出错
- 在数组中插入数据时出错 - 数组下标'int[int]'的类型无效