grave problem with ifstream::is_open

S

shawn

Hi All,
Am using MSVC6 compiler on a WinXP machine.
Am trying to read a file using std::eek:fstream;
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::eek:fstream 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::eek:fstream 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
 
L

Larry I Smith

shawn said:
Hi All,
Am using MSVC6 compiler on a WinXP machine.
Am trying to read a file using std::eek:fstream;
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::eek:fstream 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::eek:fstream 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'
 
D

David White

shawn wrote:

"grave" sounds pretty serious. :)
Hi All,
Am using MSVC6 compiler on a WinXP machine.
Am trying to read a file using std::eek:fstream;

std::eek:fstream 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::eek:fstream 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::eek:fstream 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
 
S

shawn

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;
}

}
 
D

David White

shawn said:
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
 
S

shawn

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
 
D

David White

shawn said:
// 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
 
O

Old Wolf

shawn said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top