c ++如何将文件读入动态数组并访问数组元素
c++ How to Reading file in to dynamic array and accessing array elements
我正在学习如何将 fstream
与 dynamic array
一起使用,该程序应该打印出 txt file
中的所有数据,并且可以执行排序、删除性别或计算等操作平均水平。 但结果代码很乱,数组很难访问。
这是用来练习的文本文件。
代码:
struct studentData{
string branch;
string name;
char gender;
double grade;
int number;
int total;
};
int main() {
int line_count = 0;
ifstream file_in;
int couter = 0;
file_in.open("student.txt");
if(!file_in.good())
{
cout << "Eror, could not open the file." << endl;
file_in.clear();
return -1;
}
line_count = openFileTest(file_in);
file_in.clear();
file_in.seekg(ios::beg);
studentData* p_studentData = new studentData[line_count];
loadStudentData(file_in, p_studentData,couter);
displayStudentData(p_studentData, line_count,couter);
delete [] p_studentData;
file_in.close();
return 0;
}
int openFileTest(ifstream& file_in)
{
string temp;
int linecount = 0;
while(getline(file_in, temp))
{
linecount ++;
}
return linecount;
}
void loadStudentData(ifstream& file_in,studentData* p_studentData,int couter)
{
int temp ;
file_in >> p_studentData -> total ;
temp = p_studentData->total;
for(int i=0;i<temp;i++)
{
file_in >> p_studentData->branch >> p_studentData->number ;
for(int k=0;k<p_studentData[i].number;k++)
{
file_in >> p_studentData[k].name >> p_studentData[k].gender >> p_studentData[k].grade ;
}
p_studentData++;
}
return;
}
void displayStudentData(studentData* p_studentData, int count_line,int couter)
{
cout << p_studentData->total << endl;
int temp = p_studentData->total ;
for(int i=0;i<temp;i++)
{
cout << p_studentData[i].branch << " " ;
cout << p_studentData[i].number << endl;
for(int j=0;j<p_studentData[i].number;j++)
{
cout << p_studentData[j].name << " " << p_studentData[j].gender << " " << p_studentData[j].grade << endl;
}
}
return;
}
我得到的输出是:
您似乎没有仔细跟踪您在文件中的位置。我不知道为什么 arrays[] 在 vector 可以做这些类型的分配时如此受欢迎,但请注意您缺少抽象级别:
您的代码:学生数组
作业:主题数组;每个科目都是一组学生
使用类型来跟踪这一点通常很有帮助:
struct student
{
// as above
};
struct subject
{
string name;
int number_of_students;
student* students;
};
struct all_subjects
{
int number_of_subjects;
subject* subjects;
};
利用这个,我们可以解密文件:
all_subjects load_all_subjects()
{
ifstream f( ... );
...
all_subjects all;
f >> all.number_of_subjects;
all.subjects = new subject[ all.number_of_subjects ];
for (int n = 0; n < all.number_of_subjects; n++)
load_one_subject( f, all.subjects[ n ] );
return all_students;
}
void load_one_subject( std::ifstream& f, subject& one_subject )
{
f >> one_subject.name;
f >> one_subject.number_of_students;
one_subject.students = new student[ one_subject.number_of_students ];
for (int n = 0; n < one_subject.number_of_students; n++)
load_one_student( f, one_subject.students[ n ] );
}
等等。祝你好运!
我正在学习如何将 fstream
与 dynamic array
一起使用,该程序应该打印出 txt file
中的所有数据,并且可以执行排序、删除性别或计算等操作平均水平。 但结果代码很乱,数组很难访问。
这是用来练习的文本文件。
代码:
struct studentData{
string branch;
string name;
char gender;
double grade;
int number;
int total;
};
int main() {
int line_count = 0;
ifstream file_in;
int couter = 0;
file_in.open("student.txt");
if(!file_in.good())
{
cout << "Eror, could not open the file." << endl;
file_in.clear();
return -1;
}
line_count = openFileTest(file_in);
file_in.clear();
file_in.seekg(ios::beg);
studentData* p_studentData = new studentData[line_count];
loadStudentData(file_in, p_studentData,couter);
displayStudentData(p_studentData, line_count,couter);
delete [] p_studentData;
file_in.close();
return 0;
}
int openFileTest(ifstream& file_in)
{
string temp;
int linecount = 0;
while(getline(file_in, temp))
{
linecount ++;
}
return linecount;
}
void loadStudentData(ifstream& file_in,studentData* p_studentData,int couter)
{
int temp ;
file_in >> p_studentData -> total ;
temp = p_studentData->total;
for(int i=0;i<temp;i++)
{
file_in >> p_studentData->branch >> p_studentData->number ;
for(int k=0;k<p_studentData[i].number;k++)
{
file_in >> p_studentData[k].name >> p_studentData[k].gender >> p_studentData[k].grade ;
}
p_studentData++;
}
return;
}
void displayStudentData(studentData* p_studentData, int count_line,int couter)
{
cout << p_studentData->total << endl;
int temp = p_studentData->total ;
for(int i=0;i<temp;i++)
{
cout << p_studentData[i].branch << " " ;
cout << p_studentData[i].number << endl;
for(int j=0;j<p_studentData[i].number;j++)
{
cout << p_studentData[j].name << " " << p_studentData[j].gender << " " << p_studentData[j].grade << endl;
}
}
return;
}
我得到的输出是:
您似乎没有仔细跟踪您在文件中的位置。我不知道为什么 arrays[] 在 vector 可以做这些类型的分配时如此受欢迎,但请注意您缺少抽象级别:
您的代码:学生数组
作业:主题数组;每个科目都是一组学生
使用类型来跟踪这一点通常很有帮助:
struct student
{
// as above
};
struct subject
{
string name;
int number_of_students;
student* students;
};
struct all_subjects
{
int number_of_subjects;
subject* subjects;
};
利用这个,我们可以解密文件:
all_subjects load_all_subjects()
{
ifstream f( ... );
...
all_subjects all;
f >> all.number_of_subjects;
all.subjects = new subject[ all.number_of_subjects ];
for (int n = 0; n < all.number_of_subjects; n++)
load_one_subject( f, all.subjects[ n ] );
return all_students;
}
void load_one_subject( std::ifstream& f, subject& one_subject )
{
f >> one_subject.name;
f >> one_subject.number_of_students;
one_subject.students = new student[ one_subject.number_of_students ];
for (int n = 0; n < one_subject.number_of_students; n++)
load_one_student( f, one_subject.students[ n ] );
}
等等。祝你好运!