我很難理解如何將文件傳遞給函數。如何將文件傳遞給函數?
我有一個文件有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