Click to See Complete Forum and Search --> : IOS stream loss of data in transit?


Macgoober
October 6th, 2009, 07:43 PM
Using the code below....is it possible that data could be lost? Please advise on how it could be lost, in what circumstances, and how to get around this problem...

std::ifstream in((dirCom.c_str()),
ios_base::in | ios_base::binary);
std::ofstream out(strFinal.c_str(),
ios_base::out | ios_base::binary);




char buf[BUF_SIZE];

do {
in.read(&buf[0], BUF_SIZE);
out.write(&buf[0], in.gcount( ));
} while (in.gcount( ) > 0);




in.close( );
out.close( );


so could the described above cause a loss of data in any circumstances? Because after running a function of the sort, all the files in a shared folder became unusable, they all had a file size of 0bytes...

Why would this occur?

could it have something to do with using SetAttributes()? Can setattributes even cause a loss of information? As far as I know setattributes only manipulates file flags.... not actual data...

Any help / advice in this matter is much appreciated!

Regards,

- Mac

Codeplug
October 7th, 2009, 11:16 AM
You're missing error checking, which may reveal the issue.

ifstream in("in.dat", ios::in | ios::binary);
ofstream out("out.dat", ios::out | ios::binary);

if (!in || !out)
{
cerr << "Failed to open file(s)" << endl;
return 1;
}//if

out << in.rdbuf();
if (!out)
{
cerr << "Failed to copy file" << endl;
return 1;
}//if

in.close( );
out.close( );gg