please expalin me detail these set of code spacially ofstream output(dest.c_str(
ID: 3538008 • Letter: P
Question
please expalin me detail these set of code spacially ofstream output(dest.c_str());
what is mean by dest.c_str() i know dest is string but plz explain .c_str() in detail
and if there is any alternative easy way to write these code plese do it and explaine it
void Encrypt(string source, string dest, ostream& log)
{
ifstream input(source.c_str());
if (!input)
{
cout << "Unable to open source file ";
exit(1);
}
ofstream output(dest.c_str());
if (!output)
{
cout << "Unable to open dest file ";
exit(1);
}
log << "Encrypt " << source << " " << dest << endl;
char c;
c = input.get();
while(input.good())
{
output.put(c+50);
c = input.get();
}
input.close();
output.close();
Explanation / Answer
`ofstream output(dest.c_str());` This function opens a file and output is the output stream of file, Now problem in `ofstream output(dest)` is type of `dest` is `string` but `ofstream` excepts file name only in char*. `c_str()` simply return the char* buffer pointer. I hope this will answer your question.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.