468,497 Members | 1,869 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

How to read from file descriptors.

10
Hello all,

Hello all,
iam working on some project in which i need to open a file descriptor and send it to the streambuf,
which can be done like this

Code:
Expand|Select|Wrap|Line Numbers
  1. ifstream f(fd);
  2. streambuf * buf2=f.rdbuf();
but main problem is that they are casting to

Code:
Expand|Select|Wrap|Line Numbers
  1. ifstream f((const char *)fd)
  2. streambuf * buf2=f.rdbuf();
because of which the file is not getting opened ,if i remove that leads to error there are using some conventions

there are many ways are also there like

Code:
Expand|Select|Wrap|Line Numbers
  1. File *ptr=fdopen(fd);
which is successfull and returning the value.
but i want the the output to be given in streambuf which other parts of my project is using,
can u tell me is there any way to convert the file pointer to the streambuf or is there anyway of reading the file so that the output will result in the streambuf

please help me.
Nov 25 '07 #1
3 5774
weaknessforcats
9,207 Expert Mod 8TB
This compiles:
Expand|Select|Wrap|Line Numbers
  1. char* fd = "hello";
  2. ifstream f((const char *)fd);
  3. streambuf * buf2=f.rdbuf();
  4.  
I guess I don't see your problem.

However, you should avoid the C file system: fopen, fread, etc.... C++ uses inheritance with the streams and by using the C file system you lose all the advantages of C++ streams and go back to the file handling of 1972.

As a general rule, avoid C in C++.
Nov 25 '07 #2
vin029
10
Hello thank u for reply ,
but as my question is here file descriptor not the character array,
ifstream s((char *)fd);
here the fd returns interger value, so is there anyway of getting the contents of fd so that they can be stored in the streambufer, please help me.
Nov 26 '07 #3
weaknessforcats
9,207 Expert Mod 8TB
ifstream s((char *)fd);
here the fd returns interger value, so is there anyway of getting the contents of fd so that they can be stored in the streambufer, please help me.
fd is a function??

fd is a function that returns an int??

Maybe you better show me what fd is. In my example fd was a C-string containing the disc file name to open.
Nov 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by François Pinard | last post: by
1 post views Thread by Uwe Mayer | last post: by
5 posts views Thread by John Marshall | last post: by
5 posts views Thread by Pete | last post: by
6 posts views Thread by Samuel M. Smith | last post: by
36 posts views Thread by Roman Mashak | last post: by
13 posts views Thread by poddar | last post: by
15 posts views Thread by Matt Brandt | last post: by
5 posts views Thread by yinglcs | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.