File descriptor turns to 0!

  • Thread starter Siddharth Taneja
  • Start date
S

Siddharth Taneja

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
 
V

Victor Bazarov

Siddharth said:
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
 
K

Karl Heinz Buchegger

Siddharth said:
[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
 
M

Mike Wahler

Siddharth Taneja said:
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 said:
}


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
 
M

Mike Wahler

Karl Heinz Buchegger said:
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
 
J

Jack Klein

On 5 Oct 2004 08:29:22 -0700, (e-mail address removed) (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.
 
M

Mike Wahler

Jack Klein said:
On 5 Oct 2004 08:29:22 -0700, (e-mail address removed) (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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top