0
votes

I'm trying to run a simple file I/O handling program in C++ on VS 2010. However, when i try to run it, I encounter an error that has something to do with fstream. The program is fairly simple and comprises of opening 2 text files and copying the text from the first one to the second one with a minor change. I'm pretty sure I have the right logic but I'm not sure what I am doing wrong. Any help would be much appreciated. Thanks. Here's the code:

#include<fstream>
#include<iostream>
#include<cstdlib>

using namespace std;

void file1(ifstream& in_stream, ofstream out_stream);

int main()
{
ifstream fin;
ofstream fout;
char name1[60];
cout<<"Enter the name of the input file: "<<endl;
cin>>name1;
fin.open(name1);
if(fin.fail())
{
    cout<<"Failed to open Input file"<<endl;
    exit(1);
}
else
{
    cout<<"Input file opened successfully"<<endl;
}
char name2[60];
cout<<"Enter the name of the output file: "<<endl;
cin>>name2;
fout.open(name2);
if(fout.fail())
{
    cout<<"Failed to open Output file"<<endl;
    exit(1);
}
else
{
    cout<<"Output file opened successfully"<<endl;
}
file1(fin, fout);
fin.close();
fout.close();
return 0;
}

void file1(ifstream& in_stream, ofstream out_stream)
{
char next;
in_stream.get(next);
while(!in_stream.eof())
{
    if(next=='A')
    {
        out_stream<<"ABC";
    }
    else
    {
        out_stream<<next;
    }
    in_stream.get(next);
}
}

This is the exact error I encounter on VS 2010:

Error 1 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream 1116 1 file

1

1 Answers

4
votes

out_stream is being passed by value, meaning an attempt is being made to invoke its copy constructor which is private: make it a reference:

void file1(ifstream& in_stream, ofstream& out_stream);