Why am I doing a "use remote" function when passing a std :: ofstream parameter as a parameter?

I have a dick std::ofstream fBinaryFile

and

void setFile( std::ofstream& pBinaryFile ) 
{
    fBinaryFile = pBinaryFile;
}

      

output:

 Data.h:86:16: error: use of deleted functionstd::basic_ofstream<char>& std::basic_ofstream<char>::operator=(const
 std::basic_ofstream<char>&)’
     fBinaryFile = pBinaryFile;
                 ^

      

I realized that a copy in is std::ofstream

not allowed and perhaps I am missing something. Is it possible to save content pBinaryFile

to fBinaryfile

?

+3


source to share


2 answers


Since the corresponding operator is declared as

ofstream& operator= (const ofstream&) = delete;

      



which means it is explicitly prohibited, so ofstream

semantics are used to support copying.

Depending on your architecture, you can keep the pointer / link or move it.

+6


source


If you want to copy the contents of pBinaryFile to fBinaryFile, you need to declare pBinaryfile as ifstream (input file stream), not stream (output file stream) It should look something like this:

std::ifstream pBinaryFile;
std::ofstream fBinaryFile;
std::stringstream sstream;
std::string line
pBinaryFile.open(pBinaryFileName.c_str());
fBinaryFile.open(fBinaryFileName.c_str());
if (pBinaryFile.isopen()) {
    while (pBinaryFile.good()) {
        getline(pBinaryFile, line);
        fBinaryFile << sstream(line) << endl;
    }
}
pBinaryFile.close();
fBinaryFile.close();

      

Note that pBinaryFileName and fBinaryFileName refer to your file paths.



There may be errors in this code, but I think the solution is similar to this.

I suggest this for further reading:

http://www.cplusplus.com/doc/tutorial/files/

0


source







All Articles