By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

File descriptor turns to 0!

P: n/a
Hi,

I have a very simple prg over here, trying to read the lines of a file

#include <iostream>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main() {

char tempn[100];

char line[40];
fstream f;
sprintf(tempn,"%s/%s\0","/root/somedir/prj1/","temp.cfg");
f.open(tempn,ios::in); // open file for reading
if(!f) {
cout<<"Could not find network.cfg in the directory"; // couldnt open
it
}
while(f.getline(line,10)) {

cout<<" i am in"<<endl;
if(f.eof())
break;
}
}
/* */ cout<<"f "<<f<<endl; //what is the file descpr
f.close();
delete [] tempn;
return 1;
}
when i run this prg and try to read a file of 6 lines, it prints 'i am
in' 6 times but the file descrp. is printed out as 0. While if I print
the value of the file descrp within the loop, it is ok.Why is that
happening?

Thanks

Sidhu
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Siddharth Taneja wrote:
I have a very simple prg over here, trying to read the lines of a file

#include <iostream>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main() {

char tempn[100];

char line[40];
fstream f;
sprintf(tempn,"%s/%s\0","/root/somedir/prj1/","temp.cfg"); ^ ^
You seem to have an extra slash in there, no?



f.open(tempn,ios::in); // open file for reading
if(!f) {
cout<<"Could not find network.cfg in the directory"; // couldnt open
it
Actually the file name seems to be 'temp.cfg', not 'network.cfg', the
error message is misleading.
}
while(f.getline(line,10)) {

cout<<" i am in"<<endl;
if(f.eof())
break;
}
}
/* */ cout<<"f "<<f<<endl; //what is the file descpr
Why are you trying to print 'f'? It's not "file descpr". It's a stream
object. What do you expect to see?
f.close();
delete [] tempn;
return 1;
}
when i run this prg and try to read a file of 6 lines, it prints 'i am
in' 6 times but the file descrp. is printed out as 0.
So? What do you expect?
While if I print
the value of the file descrp within the loop, it is ok.Why is that
happening?


Most likely the 'f' is converted to 'void*' and you see the result of
the converion (null pointer) printed. The reason for the null pointer
is simple: the stream is not in 'good' condition, it's in EOF state.

Once again: it's not a "file descriptor". There is no "file descriptor"
in standard C++. There are "file pointers" (FILE*) from the C library
and there are "file streams".

Victor
Jul 22 '05 #2

P: n/a
Siddharth Taneja wrote:
[snip]
when i run this prg and try to read a file of 6 lines, it prints 'i am
in' 6 times but the file descrp. is printed out as 0. While if I print
the value of the file descrp within the loop, it is ok.Why is that
happening?


What you see is *not* the file descriptor, whatever that may be.
What you see is the return value, when you use a stream object
in a boolean context (*). And that is: the streams overall state.
That is: non-zero if the stream is ready to be used, 0 if the stream
has gone into a fail state.

(*) this isn't entirely correct either, since streams dont' have a
conversion operator to bool. But they do have a conversion operator
to void* which serves the same purpose.
BTW: Your usage of eof() is wrong. In C++ eof() becomes true only
after you try *and* failed to read past the end of file. So the
typical usage pattern is this:

while( data_can_be_read ) {
process_data
}

// loop has terminated, figure out why

if( !eof() )
error file read terminated before eof was reached
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

P: n/a
"Siddharth Taneja" <si**************@gmail.com> wrote in message
news:52**************************@posting.google.c om...
Hi,

I have a very simple prg over here, trying to read the lines of a file

#include <iostream>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main() {

char tempn[100];

char line[40];
fstream f;
sprintf(tempn,"%s/%s\0","/root/somedir/prj1/","temp.cfg");
Note that the '\0' in your format string is not necessary,
'sprintf()' will terminate the string for you.


f.open(tempn,ios::in); // open file for reading
if(!f) {
cout<<"Could not find network.cfg in the directory"; // couldnt open
it
}
while(f.getline(line,10)) {

cout<<" i am in"<<endl;
if(f.eof())
break;
}
}
/* */ cout<<"f "<<f<<endl; //what is the file descpr
C++ does not define anything called a 'file descriptor'.
All i/o is done with 'streams of characters'. 'f' is
a stream object. It doesn't have a 'value' in the normal
sense of the word. So trying to output this 'value' doesn't
really mean anything. What 'value' were you expecting?

There is a stream member function ('operator void*()') which
will automatically convert to type 'bool', when the stream's
name is used in a boolean context (used to check the stream
state). I'm not sure if it's really valid for an implementation
to do this conversion in the context of inserting a stream
into a stream (which is essentially what you're trying to do),
but VC++ does give an error for that:

error C2679: binary '<<' : no operator defined which takes a
right-hand operand of type 'class std::basic_fstream<char,struct
std::char_traits<char> >' (or there is no acceptable conversion)

It appears your compiler is simply outputting the stream state
as converted to 'bool' , either zero or one. Since your 'while'
loop will only terminate when the stream state evaluates to false,
this is why you're seeing zero for the output.
f.close();
This won't work since the stream is in 'fail' state at this
point. Call 'f.clear()' first.


delete [] tempn;
This is a very serious error. You did not allocate 'tempn'
with 'new[]'. Calling 'delete[]' on it gives undefined behavior.
The array 'tempn' is an 'automatic' object. It's memory is
automatically allocated when its scope is entered, and automatically
deallocated when the scope is exited.
return 1;
This is a nonstandard value to return from 'main()'. The only
defined values for this are zero, 'EXIT_SUCCESS', and 'EXIT_FAILURE',
(those last two are macros declared by <cstdlib> (or <stdlib.h>).
}
when i run this prg and try to read a file of 6 lines, it prints 'i am
in' 6 times
That's because for six iterations of your loop, the stream is in
'good' state, as indicated by the return value of 'f.getline()'.
but the file descrp.

C++ does not define anything called 'file descriptor'.
is printed out as 0. While if I print
the value of the file descrp within the loop, it is ok.Why is that
happening?
See above. You're simply seeing the stream state as converted to type
'bool'. Again, I'm not sure if your statement:
/* */ cout<<"f "<<f<<endl; //what is the file descpr


is even legal.

-Mike
Jul 22 '05 #4

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Siddharth Taneja wrote:

BTW: Your usage of eof() is wrong.


I missed that. And you missed the misuse of 'delete[]'.

Put our two heads together, and maybe we can make a working
program. :-)

-Mike

Jul 22 '05 #5

P: n/a
On 5 Oct 2004 08:29:22 -0700, si**************@gmail.com (Siddharth
Taneja) wrote in comp.lang.c++:

In addition to what others have pointed out, there is a very serious
mistake in your program:

[snip]
int main() {

char tempn[100];
[snip]
delete [] tempn;


'tempn' is an automatic array, not allocated with new []. Calling
delete [] on it causes undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #6

P: n/a

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:ci********************************@4ax.com...
On 5 Oct 2004 08:29:22 -0700, si**************@gmail.com (Siddharth
Taneja) wrote in comp.lang.c++:

In addition to what others have pointed out, there is a very serious
mistake in your program:

[snip]
int main() {

char tempn[100];


[snip]
delete [] tempn;


'tempn' is an automatic array, not allocated with new []. Calling
delete [] on it causes undefined behavior.


I pointed this out in my reply. I guess you missed it.

-Mike
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.