无法将文件流作为函数参数传递?
Can't pass filestream as function parameter?
这是我的作业代码。每当我尝试编译时,由于 "ios_base.h" 中的某些内容,我的读取函数都会出错 我不知道该怎么做 and/or 如果我的代码执行了获取文件并将其元素移动到其中的预期功能一个单独的文件,名称和平均值彼此相邻。
#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;
}
}
流对象不可复制。他们的复制构造函数被删除。它们必须通过引用传递,而不是通过值传递:
int read(ifstream &, Student s[]);
void print(ofstream &fout, Student s[], int amount);
等...
的回答是正确的,但是他没有提到为什么流对象不允许你复制它们。
这里的问题是流对象拥有一个缓冲区,并且无法安全地复制缓冲区。
举个例子,假设您有数据通过网络套接字传入,并且在它前面有一个缓冲区,并且您复制了这个缓冲 reader。如果您从副本中读取,它将读取一些不确定数量的数据并将其放入缓冲区。该数据现在已从网络套接字中消失,仅存在于缓冲区中。现在假设您从副本中阅读。然后,您将获得一些不确定数量的数据,这些数据位于您在原始数据中读取的数据之后。以这种方式来回移动,您会得到两个“流”,其中另一个 reader 正在读取数据。
这是我的作业代码。每当我尝试编译时,由于 "ios_base.h" 中的某些内容,我的读取函数都会出错 我不知道该怎么做 and/or 如果我的代码执行了获取文件并将其元素移动到其中的预期功能一个单独的文件,名称和平均值彼此相邻。
#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;
}
}
流对象不可复制。他们的复制构造函数被删除。它们必须通过引用传递,而不是通过值传递:
int read(ifstream &, Student s[]);
void print(ofstream &fout, Student s[], int amount);
等...
这里的问题是流对象拥有一个缓冲区,并且无法安全地复制缓冲区。
举个例子,假设您有数据通过网络套接字传入,并且在它前面有一个缓冲区,并且您复制了这个缓冲 reader。如果您从副本中读取,它将读取一些不确定数量的数据并将其放入缓冲区。该数据现在已从网络套接字中消失,仅存在于缓冲区中。现在假设您从副本中阅读。然后,您将获得一些不确定数量的数据,这些数据位于您在原始数据中读取的数据之后。以这种方式来回移动,您会得到两个“流”,其中另一个 reader 正在读取数据。