-2
我的代码是用于从文本文件中读取数据的,我希望此代码从文本文件逐行读取数据,目前它只是读取我的文本文件的第一行,而不会到下一行。 “学生”是文本文件。和文本文件包含下面给定的数据,从文件中读取多行文件
[ALI,DBA,BITF11M001,3.0,3.57
ASIF,DBA,BITF11M002,3.5,3.9
ALI,OOP,BITF11M001,3.5,3.57
ASIF,OOAD,BITF11M002,3.7,3.9
ALI,OOAD,BITF11M001,3.5,3.57
ASIF,OOP,BITF11M002,4.0,3.9
ALI,DSA,BITF11M001,3.0,3.57
ASIF,DSA,BITF11M002,4.0,3.9 ]
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
struct student_attribute
{
string name [20];
string course [20];
string roll_no[20];
float GPA [20];
float CGPA [20];
};
int main()
{
int i =0;
int count ;
student_attribute data;
ifstream myfile ("Students.txt");
string line;
while (myfile)
{
getline(myfile, data.name[i], ',');
getline(myfile, data.course[i], ',');
getline(myfile, data.roll_no[i], ',');
myfile >> data.GPA[i];
myfile >> data.CGPA[i];
count++;
}
myfile.close();
for(int index = 0; index < count; index++)
{
cout << data.name[index] << " ";
}
for(int index = 0; index < count; index++)
{
cout << data.course[index] << " ";
}
for(int index = 0; index < count; index++)
{
cout << data.roll_no[index] << " ";
}
for(int index = 0; index < count; index++)
{
cout << data.GPA[index] ;
}
return 0;
}
''''总是'0' – Hacketo
你永远不会改变你的循环中的'i'的值,所以你把所有的东西都读入同一个数组元素 – Galik
'拥有'student_attribute'数组比通常情况下更有意义'student_attribute',每个成员都是一个数组。 – crashmstr