C++ 读取访问冲突,错误代码0xFFFFFFFFFFFFFF
很抱歉,这是一个重复的问题,但似乎没有解决方案适用于我的代码 这是学校的作业,从文件中读取数据并将数据复制到数组中。每次我试图编辑main中的数组“arr”时,都会引发异常 这是我的密码:C++ 读取访问冲突,错误代码0xFFFFFFFFFFFFFF,c++,file,exception,struct,C++,File,Exception,Struct,很抱歉,这是一个重复的问题,但似乎没有解决方案适用于我的代码 这是学校的作业,从文件中读取数据并将数据复制到数组中。每次我试图编辑main中的数组“arr”时,都会引发异常 这是我的密码: #include <iostream> #include <fstream> using namespace std; struct Student { string name; float gpa; int id; }; void PrintStuden
#include <iostream>
#include <fstream>
using namespace std;
struct Student {
string name;
float gpa;
int id;
};
void PrintStudents(Student arr[], int nstudents) {
for (int i = 0; i < nstudents; i++) {
cout << "Student name: " << arr[i].name << endl;
cout << "Student GPA: " << arr[i].gpa << endl;
cout << "Student ID: " << arr[i].id << endl;
}
}
int ReadStudents(string fname, Student arr[]) {
ifstream file;
file.open(fname);
int counter = 0;
string name_local;
float gpa_local;
int id_local;
int index = 0;
while (!file.eof()) {
if (counter == 0) {
file >> name_local;
}
else if (counter == 1) {
file >> gpa_local;
}
else if (counter == 2) {
file >> id_local;
}
counter++;
if (counter == 3) {
counter = 0;
Student newStudent = { name_local, gpa_local, id_local };
arr[index] = newStudent;
index++;
}
}
file.close();
return index;
}
void fillStudentArray(Student array[], int array_size) {
Student temp = { "", 0, 0 };
for (int i = 0; i < array_size; i++) {
array[i] = temp;
}
return;
}
int main() {
Student arr[128];
fillStudentArray(arr, 128); // exception thrown here??
cout << "Array filled." << endl;
cout << "Reading students" << endl;
int nstudents = ReadStudents("csci10.hw8.students.txt", arr);
PrintStudents(arr, nstudents);
return 0;
}
#包括
#包括
使用名称空间std;
结构学生{
字符串名;
浮动gpa;
int-id;
};
无效打印学生(学生arr[],内部学生){
对于(int i=0;i您没有检查文件是否已成功打开。请尝试以下操作:
ifstream file( fname );
if ( !file )
return -1;
读取时不需要局部变量。直接在数组元素中读取:
file >> arr[index].name
ReadStudents
忽略传递的数组的大小:如果读取的数组超过分配的大小(再次读取此值),则可能会遇到问题。如果允许,您可以使用std::vector
。或者,也可以传递大小–与填充的方式相同
<> >从文件中读取的方式过于复杂。尝试一个更为C++的方法:
- 为
学生定义提取运算符
:
- 像读取整数一样使用它:
或者,您可以使用:
is >> arr[ index ].name >> arr[ index ].gpa >> arr[ index ].id
你会得到这样的结果:
int ReadStudents( string fname, Student arr[]/*how large is the array?*/ )
{
ifstream file( fname );
if ( !file )
return -1;
int index = 0;
while ( file >> arr[ index ].name >> arr[ index ].gpa >> arr[ index ].id )
index++;
return index;
}
解释
如果流未成功打开,则会出现无止境的循环。您永远不会检查读取操作是否成功。如果未成功,则永远不会达到eof,而是不断递增index
,并写入超出范围的数组索引。这里的计数器也是多余的
此外,我建议使用a而不是数组,并使用push_back()
。这样可以确保不会写得越界
(可能的)解决办法
此循环:
while (!file.eof()) {
if (counter == 0) {
file >> name_local;
}
else if (counter == 1) {
file >> gpa_local;
}
else if (counter == 2) {
file >> id_local;
}
counter++;
if (counter == 3) {
counter = 0;
Student newStudent = { name_local, gpa_local, id_local };
arr[index] = newStudent;
index++;
}
}
应(使用函数定义)更改为:
int ReadStudents(字符串fname、std::vector和vec)
{
//溪流等。
while(文件>>名称\u本地>>gpa\u本地>>id\u本地){
学生新闻学生={name_local,gpa_local,id_local};
向量推回(新闻学生);
}
//清理
}
要进一步解释while(file>>name\u local>>gpa\u local>>id\u local)
的作用:
由于返回对流本身的引用,因此可以将这些语句链接在一起
如图所示,最后一个引用隐式转换为bool
(或c++11或更早版本中的void*
)。如果最后一次读取操作成功(soname\u local
、gpa\u local
和id\u local
现在具有有效值),并且流已准备好进行IO操作,则此值为true(所以它在读取时没有到达eof)。这意味着它也在检查流是否被打开
满足这些条件后,您可以创建一个新元素并将其推入向量。可能与整个循环有关,可以简化,不需要计数器。@RobertHarvey它正在复制。fillStudentArray
在我看来很好。一个问题是您没有检查文件是否已成功打开。@RobertHarvey是的。C++默认使用值语义。这只填充了一个空的结构128个副本。不相关:如果你给了<代码>学生<代码>一个产生空的默认构造函数。学生,没有必要使用fillStudentArray
。创建数组会对你有帮助,我不明白所有这些。我得到了文件>>arr[index],但我必须尝试一下。我会检查文件是否也成功打开。什么是!文件?“不是文件”!文件
的意思是“未成功打开”。看起来它可以很好地打开文件并正确读取,我只是无法写入数组。另一个用户建议使用不同的编译器,因此我将尝试下一步。我的旧代码在G++中不起作用,但您的代码起作用!感谢您的帮助,现在我只需要弄清楚它是如何工作的……我使用的编译器与您相同:无异常抛出n在第二行。
int ReadStudents( string fname, Student arr[]/*how large is the array?*/ )
{
ifstream file( fname );
if ( !file )
return -1;
int index = 0;
while ( file >> arr[ index ].name >> arr[ index ].gpa >> arr[ index ].id )
index++;
return index;
}
while (!file.eof()) {
if (counter == 0) {
file >> name_local;
}
else if (counter == 1) {
file >> gpa_local;
}
else if (counter == 2) {
file >> id_local;
}
counter++;
if (counter == 3) {
counter = 0;
Student newStudent = { name_local, gpa_local, id_local };
arr[index] = newStudent;
index++;
}
}
int ReadStudents(string fname, std::vector<Student> &vec)
{
// open stream, etc.
while (file >> name_local >> gpa_local >> id_local) {
Student newStudent = { name_local, gpa_local, id_local };
vec.push_back(newStudent);
}
// cleanup
}