C++ 如何将文件传递给函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5616476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:32:27  来源:igfitidea点击:

How to pass a file into a function?

c++visual-c++

提问by Mickey

i am having difficulty understanding how to pass a file into a function.

我很难理解如何将文件传递给函数。

i have a file with 20 names and 20 test scores that needs to be read by a function. the function will then assign the names and scores to a structure called student.

我有一个包含 20 个名称和 20 个测试分数的文件,需要由函数读取。然后,该函数会将姓名和分数分配给名为 student 的结构体。

my question is how would i write a function call with the appropriate parameters. ? to make my function read the data in the file. thanks.

我的问题是如何编写具有适当参数的函数调用。? 让我的函数读取文件中的数据。谢谢。

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;
}

回答by Ferruccio

The way you pass an ifstreaminto the function is perfectly fine.

您将 an 传递ifstream给函数的方式非常好。

I suspect that the problem lies in the way you are managing your array of StudentTypeand its size (numStudents). I would recommend changing your code to use an std::vectorinstead of a raw array. In general, you should always prefer vectors over arrays unless you have a really good reason to use an array.

我怀疑问题在于您管理数组的方式StudentType及其大小 ( numStudents)。我建议更改您的代码以使用一个std::vector而不是原始数组。通常,除非您有充分的理由使用数组,否则您应该始终更喜欢向量而不是数组。

vectors can grow to accommodate more data and keep track of their size, so you don't have to.

矢量可以增长以容纳更多数据并跟踪其大小,因此您不必这样做。

Also, it's a good idea for functions to return objects rather than modify objects passed through the parameter list.

此外,函数返回对象而不是修改通过参数列表传递的对象也是一个好主意。

#include <vector>
using namespace std;

vector<StudentType> ReadStudentData(ifstream& infile) {
    vector<StudentType> students;
    string lastName, firstName;
    int testScore;
    while (infile >> lastName >> firstName >> testScore) {
        if (testScore >= 0 && testScore <= 100) {
            StudentType student;
            student.studentName = lastName + ", " + firstName;
            student.testScore = testScore;
            students.push_back(student);
        }
    }
    return students;
}

// call the function
vector<StudentType> students = ReadStudentData(infile);

// or if you have a C++11 compiler
auto students = ReadStudentData(infile);

// use students.size() to determine how many students were read

回答by YoungLearner

The reference to the file object seems to be fine, but the array of StudentType objects maybe wrong. Try this:

对文件对象的引用似乎没问题,但 StudentType 对象的数组可能是错误的。尝试这个:

void ReadStudentData(ifstream& infile, 
std::vector<StudentType>& vecStudents, 
int& numStudents)