473,386 Members | 1,621 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,386 software developers and data experts.

File descriptor turns to 0!

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
6 1757
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
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
"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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
6
by: pembed2003 | last post by:
Hi all, Given something like: std::ofstream out_file("path"); how do I extract the file descriptor from out_file? Is it possible? What I want is to extract the file descriptor and then pass...
4
by: lynology | last post by:
I need help trying to figure why this piece of code gives me a "Bad File descriptor error" everytime I try to run it and invoke fflush. This piece of code simple outputs a char string to the output...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
32
by: Olivier | last post by:
Dear all, I thought the code ----------------------------- pt_fichier_probleme = fopen(nom_fichier, "w"); if(pt_fichier_probleme == NULL){ message_warning_s ("Erreur l'ouverture du...
3
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue...
0
ashitpro
by: ashitpro | last post by:
As per the last discussion(chapter 1), here we'll try to read the group descriptor. First of all we'll try to understand what is group descriptor. AS we know, superblock and group descriptor table...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.