473,394 Members | 1,226 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ifstream not opening

hi

I have a code, the part which is troubling goes like this
************************************************** **************
#include <istream>
#include <ostream>
#include <fstream>

using std::ifstream;
using std::ofstream;

....
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

Feb 5 '07 #1
6 4695
Gary Wessle wrote:
I have a code, the part which is troubling goes like this
************************************************** **************
#include <istream>
#include <ostream>
#include <fstream>

using std::ifstream;
using std::ofstream;

...
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 5 '07 #2
"Victor Bazarov" <v.********@comAcast.netwrites:
Gary Wessle wrote:
I have a code, the part which is troubling goes like this
************************************************** **************
#include <istream>
#include <ostream>
#include <fstream>

using std::ifstream;
using std::ofstream;

...
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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.
Feb 5 '07 #3
Gary Wessle <ph****@yahoo.comwrites:
"Victor Bazarov" <v.********@comAcast.netwrites:
Gary Wessle wrote:
I have a code, the part which is troubling goes like this
>
>
************************************************** **************
#include <istream>
#include <ostream>
#include <fstream>
>
using std::ifstream;
using std::ofstream;
>
>
>
...
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

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 $
Feb 5 '07 #4
Gary Wessle wrote:
Gary Wessle <ph****@yahoo.comwrites:
>"Victor Bazarov" <v.********@comAcast.netwrites:
>>Gary Wessle wrote:
I have a code, the part which is troubling goes like this
*********************************************** *****************
#include <istream>
#include <ostream>
#include <fstream>

using std::ifstream;
using std::ofstream;

...
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

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 <istreamfor.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 5 '07 #5
On 06 Feb 2007 03:02:43 +1100 in comp.lang.c++, Gary Wessle
<ph****@yahoo.comwrote,
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());

Feb 5 '07 #6
Gary Wessle wrote:
hi

I have a code, the part which is troubling goes like this
************************************************** **************
#include <istream>
#include <ostream>
#include <fstream>

using std::ifstream;
using std::ofstream;

...
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.
Feb 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
3
by: Earl Purple | last post by:
This is a simple function to split a file into multiple files (archives) of a fixed size. The last one will contain any remaining bytes. Here is the implementation: #include <cstdio>...
2
by: Karl | last post by:
Hey everyone! I've got a quick question on whether std::ifstream is buffered or not. The reason is that I have a homework assignment that requires me to benchmark copying files using different...
4
by: Paulo da Silva | last post by:
How can I use a ifstream object to input from stdin? Ex. ifstream inf; if (fn=="-") { what here to use inf as cin? } else { inf.open(fn.c_str,ios::in); }
2
by: Pinux | last post by:
Hi, I now run into a very strange behavior when using ifstream. Can anyone please help me out here: Basically, what I want to do is to encrypt a plain text file into a cipher text and then...
1
by: Xiaozhou.Yin | last post by:
Hi~ In the program,I first used the ifstream variable fin open the file and,open it again after called the fin.close().But the fin is fieled to open the file in the second time.The book I'm...
5
by: Johannes Bauer | last post by:
Hello group, coming from C, I'm used to somthing like this when opening a file const char *foobar = "this.txt"; FILE *f; f = fopen(foobar, "r"); if (!f) { fprintf(stderr, "Couldn't open %s:...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.