[try Beta version]
Not logged in

 
Error while opening a file

Sep 5, 2013 at 5:38pm
Hi! I have started studying about working with files but I have some problems.
I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<fstream>
using namespace std;

int main()
  {
    ofstream fout("File1.txt", ios::out);
    ifstream fin("File1.txt", ios::in);

    if(fin.good()) cout "Correct" << endl;
    else cout << "Error" << endl;

    return 0;
  }


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?
Sep 5, 2013 at 5:45pm
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.
Sep 5, 2013 at 5:47pm
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
Last edited on Sep 5, 2013 at 6:14pm
Sep 5, 2013 at 5:50pm
closed account (28poGNh0)
first alter this line cout "Correct" << endl; with this one cout << "Correct" << endl;

also if you want to in/out|puting to from/to a file in the same block ,it s better to use fstream

with the code aboce it always be "correct" either the File1 exists or not
Sep 5, 2013 at 5:53pm
@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. :)
Sep 5, 2013 at 5:54pm
Thanks @EssGeEich and @Techno01.. Good advice :)
Last edited on Sep 5, 2013 at 5:55pm
Sep 5, 2013 at 7:05pm
@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...
Last edited on Sep 5, 2013 at 7:07pm
Sep 5, 2013 at 7:10pm
It doesn't, destructors will take care of that.
Sep 5, 2013 at 7:35pm
Lol.. maybe you won't believe me but I just switched the places of
1
2
ofstream fout("File1.txt", ios::out);
ifstream fin("File1.txt", ios::in);


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 :)
Last edited on Sep 5, 2013 at 7:36pm
Sep 5, 2013 at 7:48pm
That could probably be the reason, as inverting them will first try to open the file instead of creating it first.
Topic archived. No new replies allowed.