I seem to be having a problem reading files. I am using Visual Studio Community 2013 and it will do everything but reading of files. I have checked to make sure the file being read and written is in the same directory. The following code is where I believe the problem to be:
if (inStream.bad())
{
inStream.close();
outStream.open(filename);
outStream << "This is a test file: \nWelcome to the Dark Side!";
outStream.close();
}
inStream.open(filename, ios::in);
if (inStream.good())
{
while (getline(inStream, stream[1]))
{
stream[0] += stream[1] + '\n';
}
inStream.close();
}
else
{
cout << "THIS FILE IS ROYALLY *jacked* UP!!!!" << endl;
}
and I get the "This file is royally jacked up" result. I don't understand why it's not reading. Please help.
Change the lines:
to
You don't want to call
open
on a validifstream
.Here's a sample program that demonstrates that calling
open
on a validifstream
can make it invalid.Output: