我很难理解如何将文件传递给函数。如何将文件传递给函数?
我有一个文件有20个名字和20个测试分数,需要被一个函数读取。该函数会将名称和分数分配给一个名为student的结构。
我的问题是我将如何编写一个函数调用与适当的参数。 ?使我的功能读取文件中的数据。谢谢。
CODE
// ask user for student file
cout << "Enter the name of the file for the student data to be read for input" << endl;
cout << " (Note: The file and path cannot contain spaces)" << endl;
cout << endl;
cin >> inFileName;
inFile.open(inFileName);
cout << endl;
// FUNCTION CALL how do i set this up properly?
ReadStudentData(inFile, student, numStudents);
void ReadStudentData(ifstream& infile, StudentType student[], int& numStudents)
{
int index = 0;
string lastName, firstName;
int testScore;
while ((index < numStudents) &&
(infile >> lastName >> firstName >> testScore))
{
if (testScore >= 0 && testScore <= 100)
{
student[index].studentName = lastName + ", " + firstName;
student[index].testScore = testScore;
index++;
}
}
numStudents = index;
}
您目前的代码面临的问题是什么?我发现,你正在传递参数。 – iammilind 2011-04-11 04:25:10