LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2004, 01:56 AM   #1
chuanyung
LQ Newbie
 
Registered: Jan 2004
Posts: 25

Rep: Reputation: 15
Question [c++] How can I get a file descriptor from a c++ i/o stream?


Dear all,

Because of some reason, I must get a file descript from a c++ I/O stream,
for example: ifstream.

In VC++, I found it have a function ifstream::fd() to do this.
But in Linux, there is not a such function of ifstream class.
Is there not a such function in standard C++ ?
Or is there another way to get?

Thanks for your help.
Regards,
cylin.
 
Old 03-05-2004, 02:01 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I don't know much about C++ or i/o streams, but you might be able to use fileno(). It works on 'FILE *' objects anyway (i.e. fd = fileno(stdin)).
 
Old 03-05-2004, 02:17 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I found this in the fstream.h header file. Maybe it's useful to you:

Code:
extern "C++" {
class fstreambase : virtual public ios {
#ifdef _IO_NEW_STREAMS
    mutable filebuf __my_fb; // mutable so rdbuf() can be const
#endif
    void __fb_init ();
  public:
    fstreambase();
    fstreambase(int fd);
    fstreambase(int fd, char *p, int l); /* Deprecated */
    fstreambase(const char *name, int mode, int prot=0664);
    void close();
#ifdef _IO_NEW_STREAMS
    filebuf* rdbuf() const { return &__my_fb; }
#else
    filebuf* rdbuf() const { return (filebuf*) ios::rdbuf(); }
#endif
    void open(const char *name, int mode, int prot=0664);
    int is_open() const { return rdbuf()->is_open(); }
    void setbuf(char *ptr, int len) { rdbuf()->setbuf(ptr, len); }
    void attach(int fd);
#ifdef _STREAM_COMPAT
    int filedesc() { return rdbuf()->fd(); }
    fstreambase& raw() { rdbuf()->setbuf(NULL, 0); return *this; }
#endif
};
The section I think you'd be interested in is in bold. If it's not anything useful then I'm sorry for wasting space
 
Old 03-05-2004, 10:50 AM   #4
zmieff
Member
 
Registered: Feb 2004
Location: Austria
Distribution: NetBSD 2.0, DragonFlyBSD 1.0
Posts: 66

Rep: Reputation: 15
first, include the header files

Code:
#include <fstream>
#include <iostream>
using namespace std;
then, the main function:
Code:
int main(void)
{
 ofstream output;  //this builds an instance of the class ofstream
 
 output.open("\/home\/user\/test.txt"); //open/create the file
 
 output <<"This is a Test" << endl;
       
  output.close();
  return 0;

}
hope that helps

Last edited by zmieff; 03-05-2004 at 10:52 AM.
 
Old 03-05-2004, 08:23 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I don't know the C++ classes very well, but you may want to have a look at this info-doc:

shell$ info iostream Streambuf
 
Old 03-07-2004, 03:22 PM   #6
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
The fd() function isn't in the C++ standard. C++ implementations can provide it as an extension, but you should avoid it because you can't count on it being there (as you're finding out).

Lyle
 
Old 03-08-2004, 05:41 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
As a more specific addition to what the
others have said (and because it's good
reading ;}) ....

http://www.icce.rug.nl/documents/cplusplus/

Have a look at chapter 5



Cheers,
Tink
 
Old 03-08-2004, 08:55 PM   #8
chuanyung
LQ Newbie
 
Registered: Jan 2004
Posts: 25

Original Poster
Rep: Reputation: 15
Thanks all,

I got it.

Regards,
cylin.
 
Old 03-08-2004, 11:23 PM   #9
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
thanks for that link tinkster i 've been looking for sometihg like that, most C++ tutorials seemed to be geared torwards newbs, but that one is great for C hackers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the absolute path of file using file descriptor. appas Programming 7 01-19-2012 11:47 AM
Change File Descriptor ilnli Linux - General 1 05-15-2005 05:43 PM
File Descriptor Limit rbd Linux - General 3 10-11-2004 01:57 PM
apt-file returns nothing; 'bad file descriptor' overbored Debian 3 10-03-2004 09:13 PM
File descriptor lido Linux - Newbie 5 07-17-2003 11:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration