473,715 Members | 5,945 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

grave problem with ifstream::is_op en

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_op en() fails and Tifstream.rdsta te()
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_st r(), ios::in | ios::binary);
if (Tifstream.is_o pen())
{
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 7550
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_op en() fails and Tifstream.rdsta te()
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_st r(), ios::in | ios::binary);
if (Tifstream.is_o pen())
{
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_op en() fails and Tifstream.rdsta te()
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_st r(), ios::in | ios::binary);
Is it Tofstream or Tifstream?
if (Tifstream.is_o pen())
{
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_st r(), ios::in | ios::binary);
if (Tifstream.is_o pen()) // ---- 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_st r(), ios::in | ios::binary);
if (Tifstream.is_o pen()) // ---- 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_st r(), ios::in | ios::binary);
if (Tifstream.is_o pen()) // ---- 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_st r(), ios::in | ios::binary);
if (Tifstream.is_o pen()) // ---- 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_o pen()) // ---- 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8120
by: Pete H | last post by:
I am trying to use the same ifstream object to open two files. I will eventually want to open many with a loop. The first file opens fine, but the second has a problem. In my test I try to open two .txt files, the first text.txt reads: First seems to work... text2.txt reads:
1
2638
by: Alex Vinokur | last post by:
I have several questins concerning a program below. ------ foo.cpp : BEGIN ------ #include <cassert> #include <cstdlib> #include <iostream> #include <fstream> using namespace std; int main()
8
3380
by: ambar.shome | last post by:
Hi, We are working on Solaris SunOS 5.8. We are using ifstream of iostream namespace. The snippet is given below: #include <iostream.h> #include <fstream.h> boolean readFile() { char readStr;
31
3137
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion utility in C++, before I forget everything. But it doesn't compile: ------------
13
2315
by: bob8000 | last post by:
Hi everyone I am new to C++ so any help would be fantastic. I need to know how you read a text file (with string data in) into a vector or a list. when it is read in I intend on letting the user search for a string and then deleting it from the vector
2
2197
by: monkeon | last post by:
Hi, I'm pretty new to C++ I've been doing a course for a few months. I've been trying to design a simple console application that reads in floats from a text file and returns the average, minimum and maximum figures. I can get all the functions to run separately but when I try them together the returned variables are empty. I think it's the EOF marker that is the problem but I haven't got a clue how to sort it out. The books and the...
5
1917
by: B. Williams | last post by:
I need some assistance with random access file processing. I have a function that I would like to change from sequential file processing to random access. Thanks in advance. void updatePower(string filename) { ifstream in(filename.c_str(), ios::in); int cnt = 0; // read all records into database until we reach the end of the file
6
4754
by: Gary Wessle | last post by:
hi I have a code, the part which is troubling goes like this **************************************************************** #include <istream> #include <ostream> #include <fstream>
11
8137
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream fails to open a file, but nothing I read about ifstream says anything about a reliable place to get an error message. Using stdio I can simply do: FILE *f = fopen(filename, "rb"); if (!f) perror(filename);
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9103
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2539
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2118
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.