469,890 Members | 2,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

grave problem with ifstream::is_open

Hi All,
Am using MSVC6 compiler on a WinXP machine.
Am trying to read a file using std::ofstream;
the problem is that Tofstream.is_open() fails and Tifstream.rdstate()
returns "2" which translates to "The system cannot find the file
specified.".
This is really weird because the said file am tring to read exists on
the disk at the required location.

SNIPPET:-
{
std::ofstream Tofstream; // ofstream object of the opened file ;
Tifstream.open(szFileName.c_str(), ios::in | ios::binary);
if (Tifstream.is_open())
{
while(Tifstream.good() && !(Tifstream.eof()))
{
///do stuff
}
}
else
{
cerr<<Tifstream.rdstate(); // returns 2;
}
}

Is this a known problem with std::ofstream as such ???
Can't I open the file forcefully in some mode say read + write???
does anyone has a better way to open and read the contents of a file ??

Any help in this regard is welcome
cheers

Shawn

Sep 5 '05 #1
7 7249
shawn wrote:
Hi All,
Am using MSVC6 compiler on a WinXP machine.
Am trying to read a file using std::ofstream;
the problem is that Tofstream.is_open() fails and Tifstream.rdstate()
returns "2" which translates to "The system cannot find the file
specified.".
This is really weird because the said file am tring to read exists on
the disk at the required location.

SNIPPET:-
{
std::ofstream Tofstream; // ofstream object of the opened file ;
Tifstream.open(szFileName.c_str(), ios::in | ios::binary);
if (Tifstream.is_open())
{
while(Tifstream.good() && !(Tifstream.eof()))
{
///do stuff
}
}
else
{
cerr<<Tifstream.rdstate(); // returns 2;
}
}

Is this a known problem with std::ofstream as such ???
Can't I open the file forcefully in some mode say read + write???
does anyone has a better way to open and read the contents of a file ??

Any help in this regard is welcome
cheers

Shawn


'ofstream' - OUTPUT (write to) stream
'ifstream' - INPUT (read from) stream
'fstream' - IN/OUT (read/write) stream

You want 'ifstream' rather than 'ofstream'
Sep 6 '05 #2
shawn wrote:

"grave" sounds pretty serious. :-)
Hi All,
Am using MSVC6 compiler on a WinXP machine.
Am trying to read a file using std::ofstream;
std::ofstream is an output stream. You write to it.
the problem is that Tofstream.is_open() fails and Tifstream.rdstate()
returns "2" which translates to "The system cannot find the file
specified.".
Using VC++ 6.0 also, I had no trouble getting as far as successfully opening
a file.
This is really weird because the said file am tring to read exists on
the disk at the required location.

SNIPPET:-
{
std::ofstream Tofstream; // ofstream object of the opened file ;
Tifstream.open(szFileName.c_str(), ios::in | ios::binary);
Is it Tofstream or Tifstream?
if (Tifstream.is_open())
{
while(Tifstream.good() && !(Tifstream.eof()))
{
///do stuff
}
}
else
{
cerr<<Tifstream.rdstate(); // returns 2;
}
}

Is this a known problem with std::ofstream as such ???
Can't I open the file forcefully in some mode say read + write???
does anyone has a better way to open and read the contents of a file
??


Your post is confusing in a number of places as to whether you are reading
or writing. The compiler should have given an error if you attempted read
operations such as >> on an ofstream. Your code also produced numerous
compiler errors. Please post the code again, but this time code that is
compilable without modification (so compile it, then post it).

DW
Sep 6 '05 #3
oops!! sorry,
what I meant was to say that I am reading from the file....
so my code is as below:-

snippet:-
{
std::string szFileName = "MyFile.txt"; //file to read
std::ifstream Tifstream; // ifstream object of the
opened file to read ;
Tifstream.open(szFileName.c_str(), ios::in | ios::binary);
if (Tifstream.is_open()) // ---- fails here ----
{
while(Tifstream.good() && !(Tifstream.eof()))
{
///read from file
}
}
else
{
cerr<<Tifstream.rdstate(); // returns 2;
}

}

Sep 6 '05 #4
shawn wrote:
oops!! sorry,
what I meant was to say that I am reading from the file....
so my code is as below:-

snippet:-
{
std::string szFileName = "MyFile.txt"; //file to read
std::ifstream Tifstream; // ifstream object of the
opened file to read ;
Tifstream.open(szFileName.c_str(), ios::in | ios::binary);
if (Tifstream.is_open()) // ---- fails here ----
{
while(Tifstream.good() && !(Tifstream.eof()))
{
///read from file
}
}
else
{
cerr<<Tifstream.rdstate(); // returns 2;
}

}


After creating a file called MyFile.txt and inserting the #includes and
std:: missing from the code, I compiled the program with VC++ 6.0 and ran
it. It opened the file and got stuck inside the (as posted) infinite loop.
Whatever the problem is, I don't think it's with the code or the compiler.

DW
Sep 6 '05 #5
> Tifstream.open(szFileName.c_str(), ios::in | ios::binary);
if (Tifstream.is_open()) // ---- fails here ----
{
// primary problem is that is_open() fails for incorrect reason (as specified in my earlier post)

// even though the file exists, It so happens that when i quit the
program and
// execute it afresh, It works fine. But randomly it fails for any
session.

so my query is with is_open() ......as to why it fails randomly..
a) I sit bcoz of some unclosed file handle
b) sharing voilation etc

Shawn

Sep 6 '05 #6
shawn wrote:
Tifstream.open(szFileName.c_str(), ios::in | ios::binary);
if (Tifstream.is_open()) // ---- fails here ----
{
// primary problem is that is_open() fails for incorrect reason (as
specified in my earlier post) // even though the file exists, It so happens that when i quit the
program and
// execute it afresh, It works fine. But randomly it fails for any
session.

so my query is with is_open() ......as to why it fails randomly..
a) I sit bcoz of some unclosed file handle


Unlikely.
b) sharing voilation etc


I guess that's possible, but I'd expect a violation only if something else
has it open for writing. You could test this by having some other
application open the file.

DW
Sep 6 '05 #7
shawn wrote:
if (Tifstream.is_open()) // ---- fails here ----
{
while(Tifstream.good() && !(Tifstream.eof()))
If eof() is true, then good() must be false. So you could replace
that line with just:

while (Tifstream.good())

Please read the FAQ on how to read a file correctly
(ie. looping on good() or eof() does not work, because eof()
does not return true until AFTER you have tried to read past
the end of file and failed. You should instead check each read
operation for failure.)
cerr<<Tifstream.rdstate(); // returns 2;


You could use a system-level diagnostic (eg. Filemon for windows)
to find out why a file does not open. It is unlikely to be
'random' as you suggest, there will always be a good reason
(eg. file does not exist, file is opened exclusively by another
process, you do not have read access to file, out of file handles).

Sep 7 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Pete H | last post: by
1 post views Thread by Alex Vinokur | last post: by
8 posts views Thread by ambar.shome | last post: by
31 posts views Thread by Martin Jørgensen | last post: by
13 posts views Thread by bob8000 | last post: by
5 posts views Thread by B. Williams | last post: by
6 posts views Thread by Gary Wessle | last post: by
11 posts views Thread by adramolek | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.