ifstream not opening

G

Gary Wessle

hi

I have a code, the part which is troubling goes like this


****************************************************************
#include <istream>
#include <ostream>
#include <fstream>

using std::ifstream;
using std::eek:fstream;



....
ifstream ifs(f_name.c_str());
if( ifs.is_open() )
cout << "is open" << endl;
else
cout << "closed" << endl;
....

when run into the debugger "gdb" it shows "closed", however it prints
"p f_name" fine and if I take that and copy it to the command line
"cat /full/path/to/file" it prints out. the permission on that file is
-rw-r--r--

many thanks
 
V

Victor Bazarov

Gary said:
I have a code, the part which is troubling goes like this


****************************************************************
#include <istream>
#include <ostream>
#include <fstream>

using std::ifstream;
using std::eek:fstream;



...
ifstream ifs(f_name.c_str());
if( ifs.is_open() )
cout << "is open" << endl;
else
cout << "closed" << endl;
...

when run into the debugger "gdb" it shows "closed", however it prints
"p f_name" fine and if I take that and copy it to the command line
"cat /full/path/to/file" it prints out. the permission on that file is
-rw-r--r--

"permissions" are OS-specific. "Debuggers" are tools and OS-specific.
Please post OS-speficic inquiries to the newsgroup dedicated to that OS.

V
 
G

Gary Wessle

Victor Bazarov said:
"permissions" are OS-specific. "Debuggers" are tools and OS-specific.
Please post OS-speficic inquiries to the newsgroup dedicated to that OS.

V

well, the code I posted will print "closed" where I expected it to
print out "is open".
the statements about the gdb and permissions are just to show that I
did some homework.
 
G

Gary Wessle

Gary Wessle said:
well, the code I posted will print "closed" where I expected it to
print out "is open".
the statements about the gdb and permissions are just to show that I
did some homework.


#include <sstream>
#include <istream>
#include <string>

using namespace std;

int main(){
string f_name = "../myProg/str1/some_data_file";
ifstream ifs(f_name.c_str());
if( ifs.is_open() )
cout << "is open" << endl;
else
cout << "closed" << endl;
}

****************************************************************
~/toy $ make clean; make
rm -rf *.o proj
g++ -gdwarf-2 -c -o my.o my.cpp
my.cpp: In function ¡Æint main()¡Ç:
my.cpp:9: error: variable ¡Æstd::ifstream ifs¡Ç has initializer but incomplete type
my.cpp:11: error: ¡Æcout¡Ç was not declared in this scope
my.cpp:13: error: ¡Æcout¡Ç was not declared in this scope
make: *** [my.o] Error 1
~/toy $
 
V

Victor Bazarov

Gary said:
Gary Wessle said:
well, the code I posted will print "closed" where I expected it to
print out "is open".
the statements about the gdb and permissions are just to show that I
did some homework.


#include <sstream>
#include <istream>
#include <string>

using namespace std;

int main(){
string f_name = "../myProg/str1/some_data_file";
ifstream ifs(f_name.c_str());
if( ifs.is_open() )
cout << "is open" << endl;
else
cout << "closed" << endl;
}

****************************************************************
~/toy $ make clean; make
rm -rf *.o proj
g++ -gdwarf-2 -c -o my.o my.cpp
my.cpp: In function $B!F(Bint main()$B!G(B:
my.cpp:9: error: variable $B!F(Bstd::ifstream ifs$B!G(B has initializer but
incomplete type my.cpp:11: error: $B!F(Bcout$B!G(B was not declared in this
scope
my.cpp:13: error: $B!F(Bcout$B!G(B was not declared in this scope
make: *** [my.o] Error 1
~/toy $

You need to include <fstream>, I am not sure what you need <istream> for.

V
 
D

David Harmon

On 06 Feb 2007 03:02:43 +1100 in comp.lang.c++, Gary Wessle
ifstream ifs(f_name.c_str());
if( ifs.is_open() )
cout << "is open" << endl;

My suggestion (as usual) is

ifstream ifs(f_name.c_str());
if(!ifs)
perror(f_name.c_str());
 
J

Jacek Dziedzic

Gary said:
hi

I have a code, the part which is troubling goes like this


****************************************************************
#include <istream>
#include <ostream>
#include <fstream>

using std::ifstream;
using std::eek:fstream;



...
ifstream ifs(f_name.c_str());
if( ifs.is_open() )
cout << "is open" << endl;
else
cout << "closed" << endl;
...

when run into the debugger "gdb" it shows "closed", however it prints
"p f_name" fine and if I take that and copy it to the command line
"cat /full/path/to/file" it prints out. the permission on that file is
-rw-r--r--

Maybe the file is in use already and you can't open it to read?

HTH,
- J.
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top