problem with streambuf

This is a discussion on problem with streambuf within the C++ Programming forums, part of the General Programming Boards category; Hello experts, i have one query which i am not able to solve please suggest me my problem is i ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    problem with streambuf

    Hello experts,

    i have one query which i am not able to solve please suggest me my problem is i have to open a filedescriptor and have to store the contents in streambuffer,

    i,e
    Code:
    int main () {
    
     char ch;
     streambuf * pbuf;size_t size;
      ifstream istr(const char *)fd);
      pbuf = istr.rdbuf();
    
      while (pbuf->sgetc()!=EOF)
      {
         ch = pbuf->sbumpc();
         cout << ch;
      }
    
      istr.close();
    
      return 0;
    }
    but here the line
    Code:
    ifstream istr(const char *)fd);
    is not properly interpreting thats why there is no out put

    if i simply give
    Code:
    ifstream istr(fd);
    it gives me error as this method does not exist,
    please helop me what to do but i want the result in <b>streambuffer</b> because this is the input for some other module in my project pleasesuggest what are the different way i can get so the i get the contents in <b>streambuffer.</b>

    thanks in advance.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    There is no standard C++ method of reading from a UNIX fd. You need to use the native read() function and read directly into the streambuf's buffer.

    I don't use streambufs enough to tell you how to do that. Somebody else might be able to answer better.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    problem with stream buffer

    thank u for reply,

    but please help me any experts who knows the answer as i am struck at this point very badly , any way to open the file descriptor so that the contents should gt stored in streambuffer, please help me.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    problem with streambuufer

    please any experts help me.............................

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Don't be impatient. brewbuck told you that you can't do what you're trying, unless you're improperly using the term "file descriptor".

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    alternate suggestion to this problem.

    but even i tried in this way also

    Code:
    char ch[100];
    pFile = fdopen (fd,"r");
    fgets(ch, 90,pFile);
    know is there any way i can convert character buffer to stream buffer

    please tell me if any alternate solutions for this problem.

    at the end i want the output in streambuffer.
    Last edited by vin_pll; 06-15-2008 at 11:29 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    C does not support native Linux streams and stuff.
    If you begin with an API, you need to make the entire line API.
    In short, use only APIs and not C library functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    I did this awhile ago to help someone out, but I make no claim as to its robustness. It was more of an example than anything. But, here you go:

    Code:
    #ifndef H_FDIO
    #define H_FDIO 1
    
    #include <algorithm>                                    // std::min
    #include <cerrno>                                       // errno, EINTR
    #include <cstdio>                                       // BUFSIZ, EOF
    #include <cstring>                                      // std::memcpy
    #include <istream>                                      // std::streambuf, std::[i,o,io]stream
    
    extern "C" {
    #include <unistd.h>                                     // read, write
    }
    
    namespace fdio {
            // derived streambuf class, full support for buffered reading and writing
            class streambuf : public std::streambuf {
            protected:
                    int fd;                                 // file descriptor
    
                    enum { BACKSIZE = 8 };                  // putback area size
                    enum { PUTSIZE = BUFSIZ };              // put area size
                    enum { GETSIZE = BUFSIZ };              // get area size
    
                    char pbuf[PUTSIZE];                     // put area
    
                    char gbuf[GETSIZE + BACKSIZE];          // get and putback areas
                    char *const gbase;                      // get area base pointer
    
                    // -- flush: output to destination.
    		//           returns number of characters sent,
    		//           or EOF upon failure
                    int flush() {
                            // calculate # characters to send
                            std::ptrdiff_t n = pptr() - pbase();
    
                            // send characters to destination
                            if ( write(fd, pbase(), n) != n )
                                    return EOF;
    
                            // reset put pointer
                            pbump(-n);
                            return n;
                    }
    
                    // -- overridden virtual functions
                    virtual int sync() {
                            return flush() == EOF ? -1 : 0;
                    }
    
                    virtual int overflow(int c) {
                            if ( c != EOF ) {
                                    *pptr() = c;            // use last free space (see ctor)
                                    pbump(1);
                            }
                            return flush() == EOF ? EOF : c;
                    }
    
                    virtual int underflow() {
                            if ( gptr() < egptr() )         // characters available in get area?
                                    return *gptr();
    
                            // set putback area
                            std::ptrdiff_t nback = std::min<std::ptrdiff_t>(gptr() - eback(), BACKSIZE);
                            std::memcpy(gbase - nback, gptr() - nback, nback);
    
                            // read new characters
                            ssize_t nread = read(fd, gbase, GETSIZE);
                            if ( nread <= 0 )
                                    return EOF;
    
                            // reset get area pointers
                            setg(gbase - nback, gbase, gbase + nread);
                            return *gptr();
                    }
    
            public:
                    // -- ctor
                    streambuf(int file_descriptor)
                            : fd    (file_descriptor)
                            , gbase (gbuf + BACKSIZE)
                    {
                            setg(gbase, gbase, gbase);      // trigger underflow call on first read
                            setp(pbuf, pbuf+PUTSIZE-1);     // one space left for overflow
                    }
    
                    // -- xtor
                    virtual ~streambuf() {
                            sync();
                    }
            };
    
            // derived ostream class
            class ostream : public std::ostream {
            public:
                    ostream(int fd)
                            : std::ostream  (0)
                            , sbuf          (fd)
                    { init(&sbuf); }
    
            protected:
                    fdio::streambuf sbuf;
            };
    
            // derived istream class
            class istream : public std::istream {
            public:
                    istream(int fd)
                            : std::istream  (0)
                            , sbuf          (fd)
                    { init(&sbuf); }
    
            protected:
                    fdio::streambuf sbuf;
            };
    
            // derived iostream class
            class iostream : public std::iostream {
            public:
                    iostream(int fd)
                            : std::iostream (0)
                            , sbuf          (fd)
                    { init(&sbuf); }
    
            protected:
                    fdio::streambuf sbuf;
            };
    }
    
    #endif // H_FDIO
    And a cheap example program to exercise and show it's capabilities:

    Code:
    #include "fdio.h"
    
    void hello(fdio::streambuf&);
    void world(fdio::streambuf&);
    
    int main() {
        fdio::streambuf out(1);
        hello(out);
        world(out);
        out.pubsync();
    }
    
    void hello(fdio::streambuf& out) {
        out.sputc('H');
        out.sputc('e');
        out.sputn("llo ", 4);
    }
    
    void world(fdio::streambuf& out) {
        out.sputc('W');
        out.sputn("orld!", 5);
        out.sputc('\n');
    }
    Like I said....no warranties here. I just pulled it out of some old stuff. You can use the [i,o,io]stream classes if you want formatted I/O as well. Good Luck!

    I'd recommend Josuttis' book on the C++ STL for more about using streambuf and friends. I have another book I actually like better, called Standard C++ IOStreams, but that's alot to chew at first.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    thank you very much for u r valueble inputs,

    here is the another question like:

    am trying this way also
    Code:
    streambuf * pbuf;
      ifstream istr; 
    pbuf=istr.rdbuf()->open(fd);
    this code is successfull in some times and fails some time can anyone
    tell me what is the main reason.

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by vin_pll View Post
    thank you very much for u r valueble inputs,

    here is the another question like:

    am trying this way also
    Code:
    streambuf * pbuf;
      ifstream istr; 
    pbuf=istr.rdbuf()->open(fd);
    this code is successfull in some times and fails some time can anyone
    tell me what is the main reason.
    Without anymore information, I would have to guess that you are using open wrong. I don't remember ever seeing an open member function that accepts an already open file descriptor. You will need to explain what happens when the code fails, but make sure that the open member function is okay with accepting an already open file descriptor.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    thank you whoie,
    for u r inputs when code fails it return 0 and if successfully opens it gives me no of characters in the fd.

    again ,as that of u r previous example you sent, i can do
    Code:
    ifstream f(fd);
    but i want the output in

    Code:
    streambuf *s
    which is not acceptable there please if you can simply it will be great favour even i am also trying hard for that ,
    any how thanks in adavance for helping me this much.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Hello whoie,

    thank you very much for u r fdio.h , iwas able to solve that problem ,
    now my streams are reading the file descriptors successfully.

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Quote Originally Posted by vin_pll View Post
    Hello whoie,

    thank you very much for u r fdio.h , iwas able to solve that problem ,
    now my streams are reading the file descriptors successfully.
    A word of caution from someone whose had to compile across multiple platforms: The code you have right now may not run on any other platforms that the current one you're using to work on your current project.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    thank u for u r inputs,

    even my task is to run this code platform independently , as u said code will not work please may i know the reason for this and what can be the alternate solution for this.

    expecting your response.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First step is to learn how to read what everyone here has been telling you.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21