C++ Can';是否将filestream作为函数参数传递?

C++ Can';是否将filestream作为函数参数传递?,c++,string,file,inputstream,filestream,C++,String,File,Inputstream,Filestream,这是我的作业代码。每当我尝试编译时,由于“ios_base.h”中的某些内容,我的read函数都会出现错误,我不确定该做什么和/或我的代码是否执行了预期的功能,即获取一个文件并将其元素移动到一个单独的文件中,该文件的名称和平均值相邻 #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; struct St

这是我的作业代码。每当我尝试编译时,由于“ios_base.h”中的某些内容,我的read函数都会出现错误,我不确定该做什么和/或我的代码是否执行了预期的功能,即获取一个文件并将其元素移动到一个单独的文件中,该文件的名称和平均值相邻

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

struct Student
{
    string fname;
    string lname;
    double average;
};

int read(ifstream, Student s[]);

void print(ofstream fout, Student s[], int amount);


int main()
{
    const int size = 10;
    ifstream fin;
    ofstream fout;
    string inputFile;
    string outputFile;
    Student s[size];

    cout << "Enter input filename: ";
    cin >> inputFile;
    cout << "Enter output filename: ";
    cin >> outputFile;
    cout << endl;

    fin.open(inputFile.c_str());
    fout.open(outputFile.c_str());

    read(fin , s);
    print(fout, s, read(fin, s));

}

int read(ifstream fin, Student s[])
{
    string line;
    string firstName;
    string lastName;
    double score;
    double total;
    int i=0;
    int totalStudents=0;
    Student stu;

    while(getline(fin, line)){
        istringstream sin;
        sin.str(line);

        while(sin >> firstName >> lastName){
            stu.fname = firstName;
            stu.lname = lastName;

            while(sin >> score){
            total *= score;
            i++;
            }
            stu.average = (total/i);
        }
        s[totalStudents]=stu;
        totalStudents++;
    }
    return totalStudents;
}

void print(ofstream fout, Student s[], int amount)
{
    ostringstream sout;
    for(int i = 0; i<amount; i++)
    {
        sout << left << setw(20) << s[i].lname << ", " << s[i].fname;
        fout << sout << setprecision(2) << fixed << "= " << s[i].average;
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
体类型
{
字符串fname;
字符串名称;
双倍平均;
};
int-read(ifstream,学生s[]);
空白打印(流式打印,学生s[],整数金额);
int main()
{
常数int size=10;
流鳍;
流式流量计;
字符串输入文件;
字符串输出文件;
学生人数[人数];
cout>输入文件;
输出文件;
cout>firstName>>lastName){
stu.fname=名字;
stu.lname=姓氏;
而(sin>>得分){
总分*=总分;
i++;
}
学生平均数=(总数/i);
}
学生总数=stu;
学生++;
}
返回学生;
}
空白打印(流式打印,学生s[],整笔金额)
{
奥斯汀河南区;

for(int i=0;i流对象不可复制。将删除它们的复制构造函数。它们必须通过引用传递,而不是通过值传递:

int read(ifstream &, Student s[]);

void print(ofstream &fout, Student s[], int amount);
等等。

的答案是正确的,但他没有提到为什么流对象不允许您复制它们

这里的问题是一个流对象拥有一个缓冲区,并且不能安全地复制缓冲区


举个例子,假设您有数据通过网络套接字进入,并且它前面有一个缓冲区,您复制了这个缓冲读取器。如果您从副本中读取,它将读取一些不确定的数据并将其放入缓冲区。这些数据现在从网络套接字中消失,只存在于缓冲区中。现在假设您从副本中读取广告。然后你会得到一些不确定数量的数据,这些数据来自于你在原始文件中读取的数据。以这种方式来回移动,你会得到两个“流”另一个读卡器读取数据的地方有间隙。

请包含实际的错误代码。非常感谢我将其编译。现在唯一的问题是输出文件中除了一个空行之外没有任何内容?我是否知道正确填充数组,然后将其放入输出流?您的代码中有一个错误。您调用
read()
两次,直到文件结束。然后code再次调用
read()
,并将其返回值传递给
print()
。您认为第二次调用
read()需要多少记录
将读取?噢,duh忘记了我的大小常量。我的输出文件现在返回的是内容,而不是名称和平均分数。输出应该是“Coop,Jason=27.31”,但它只是随机数字和字母的混合。