The problem is that in the book that I study from is written that if there is no such file as File1 it will be created automatically and fin.good() will be true. But the first time when there is no such file it outputs "Error". When I start the program for a second time everything is ok because the file already exists.
Can you tell me why is this happening?
Because you should close fout.
I don't think you can keep two fstreams open on the same file at once.
Call fout.close(); before declaring fin, then report back.
Running this on my schools computer this outputs "Correct" for me. I'm sure this is a mistake in posting, but you forgot the "<<" after cout.
If I had to guess I would say there's a small delay creating the file? And so it's going to if fin.good before the file is created. Try to put Sleep(10); before if fin.good
@huike I tested the code that I have posted [I wrote it here and that's why I have missed <<(I didn't try to compile it)] on another PC and it outputs "Correct". Maybe something is wrong with my PC. I will check the original code that I have written. :)
@EssGeEich I'm sure that's not the reason. You can definitely have a output and input for a file simultaneously. He's not closing the file at all right now though, but I'm not sure if that matters...
In my original code first was ifstream fin(...) and then ofstream fout(...)
And it worked.. I didn't expect that this could be the reason..
Maybe I should first create the file and then to try to access it :)