Read memory buffer via stream interface

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Woody

    Read memory buffer via stream interface

    Hi,

    for example, i have

    unsigned char buff[ 100 ]

    the buffer hold a sequences of raw bytes. My question is, what is the
    standard method of reading the `buff' via std::istream interface?

    A relative question is: how to write to the `buff' via std::ostream
    interface? I think this can be done by using std::stringstre am, but i
    am not sure of it.

    Thanks.

    -
    woody
  • kasthurirangan.balaji@gmail.com

    #2
    Re: Read memory buffer via stream interface

    On Mar 5, 2:16 pm, Steven Woody <narkewo...@gma il.comwrote:
    Hi,
    >
    for example, i have
    >
      unsigned char buff[ 100 ]
    >
    the buffer hold a sequences of raw bytes.  My question is, what is the
    standard method of reading the `buff' via std::istream interface?
    >
    A relative question is: how to write to the `buff' via std::ostream
    interface? I think this can be done by using std::stringstre am, but i
    am not sure of it.
    >
    Thanks.
    >
    -
    woody
    Use std::istringstr eam for input, std::ostringstr eam for output,
    std::stringstre am for both. Once you have created a stringstream, you
    can use istream/ostream read/write facilities on it.

    #include <iostream>
    #include <cstring>
    #include <sstream>

    main()
    {
    std::stringstre am sstr;
    sstr << "balaji";
    std::cout << sstr.str() << '\n';
    sstr.str(""); //clear in-memory data
    char buf[100];
    strcpy(buf,"asd kjabvdba");
    std::stringstre am sstr1(buf);
    std::cout << sstr1.str() << '\n';
    }

    Thanks,
    Balaji.

    Comment

    • Steven Woody

      #3
      Re: Read memory buffer via stream interface

      On Mar 5, 5:38 pm, kasthurirangan. bal...@gmail.co m wrote:
      On Mar 5, 2:16 pm, Steven Woody <narkewo...@gma il.comwrote:
      >
      >
      >
      Hi,
      >
      for example, i have
      >
      unsigned char buff[ 100 ]
      >
      the buffer hold a sequences of raw bytes. My question is, what is the
      standard method of reading the `buff' via std::istream interface?
      >
      A relative question is: how to write to the `buff' via std::ostream
      interface? I think this can be done by using std::stringstre am, but i
      am not sure of it.
      >
      Thanks.
      >
      -
      woody
      >
      Use std::istringstr eam for input, std::ostringstr eam for output,
      std::stringstre am for both. Once you have created a stringstream, you
      can use istream/ostream read/write facilities on it.
      >
      #include <iostream>
      #include <cstring>
      #include <sstream>
      >
      main()
      {
      std::stringstre am sstr;
      sstr << "balaji";
      std::cout << sstr.str() << '\n';
      sstr.str(""); //clear in-memory data
      char buf[100];
      strcpy(buf,"asd kjabvdba");
      std::stringstre am sstr1(buf);
      std::cout << sstr1.str() << '\n';
      >
      }
      >
      Thanks,
      Balaji.
      Thanks a lot! I just did not see that a stringstream can be
      constructed from a char[]. My book does not mentioned that. Where's
      a good STL reference I can find online or free?

      Another question I also like to ask here:

      After,
      char buf[100];
      strcpy(buf,"123 \n");
      std::stringstre am sstr1(buf);

      How can I let cout << sstr1.str() << endl; prints something like that:

      0x31 0x32 0x33 0x0a ?

      Thanks again.

      -
      woody

      Comment

      • kasthurirangan.balaji@gmail.com

        #4
        Re: Read memory buffer via stream interface

        On Mar 5, 4:57 pm, Steven Woody <narkewo...@gma il.comwrote:
        On Mar 5, 5:38 pm, kasthurirangan. bal...@gmail.co m wrote:
        >
        >
        >
        >
        >
        On Mar 5, 2:16 pm, Steven Woody <narkewo...@gma il.comwrote:
        >
        Hi,
        >
        for example, i have
        >
          unsigned char buff[ 100 ]
        >
        the buffer hold a sequences of raw bytes.  My question is, what is the
        standard method of reading the `buff' via std::istream interface?
        >
        A relative question is: how to write to the `buff' via std::ostream
        interface? I think this can be done by using std::stringstre am, but i
        am not sure of it.
        >
        Thanks.
        >
        -
        woody
        >
        Use std::istringstr eam for input, std::ostringstr eam for output,
        std::stringstre am for both. Once you have created a stringstream, you
        can use istream/ostream read/write facilities on it.
        >
        #include <iostream>
        #include <cstring>
        #include <sstream>
        >
        main()
        {
        std::stringstre am sstr;
        sstr << "balaji";
        std::cout << sstr.str() << '\n';
        sstr.str(""); //clear in-memory data
        char buf[100];
        strcpy(buf,"asd kjabvdba");
        std::stringstre am sstr1(buf);
        std::cout << sstr1.str() << '\n';
        >
        }
        >
        Thanks,
        Balaji.
        >
        Thanks a lot!  I just did not see that a stringstream can be
        constructed from a char[].  My book does not mentioned that.  Where's
        a good STL reference I can find online or free?
        >
        Another question I also like to ask here:
        >
        After,
          char buf[100];
          strcpy(buf,"123 \n");
          std::stringstre am sstr1(buf);
        >
        How can I let cout << sstr1.str() << endl; prints something like that:
        >
          0x31 0x32 0x33 0x0a ?
        >
        Thanks again.
        >
        -
        woody- Hide quoted text -
        >
        - Show quoted text -
        You may want to use this.

        #include <iostream>
        #include <cstring>
        #include <sstream>
        #include <algorithm>
        #include <string>

        void print(const char &c)
        {
        std::cout << std::showbase << std::hex << toascii(c) << ' ';
        }

        main()
        {
        std::stringstre am sstr;
        sstr << "balaji";
        std::cout << sstr.str() << '\n';
        sstr.str(""); //clear data
        char buf[100];
        strcpy(buf,"123 \n");
        std::stringstre am sstr1(buf);
        std::cout << sstr1.str() << '\n';
        const std::string &temp(sstr1.str ());
        std::for_each(t emp.begin(),tem p.end(),print);
        }


        You may also try for ebooks of C++ primer and The C++ Standard Library
        by Josuttis.

        Thanks,
        Balaji.

        Comment

        • James Kanze

          #5
          Re: Read memory buffer via stream interface

          On Mar 5, 2:12 pm, kasthurirangan. bal...@gmail.co m wrote:
          On Mar 5, 4:57 pm, Steven Woody <narkewo...@gma il.comwrote:
          [...]
          You may want to use this.
          >
          #include <iostream>
          #include <cstring>
          #include <sstream>
          #include <algorithm>
          #include <string>
          void print(const char &c)
          {
          std::cout << std::showbase << std::hex << toascii(c) << ' ';
          What's "toascii"? The only "toascii" I know of is a very old
          pre-standard C Unix function---from the days when isupper, etc.
          only worked for ASCII characters.

          (It's also considered good practice to restore the flags of an
          output stream after modifying them. You don't necessarily want
          all of the output in the rest of the program to be in hex.)
          }
          main()
          And of course, you'll need to declare main to return an int.
          {
          std::stringstre am sstr;
          sstr << "balaji";
          std::cout << sstr.str() << '\n';
          sstr.str(""); //clear data
          char buf[100];
          strcpy(buf,"123 \n");
          std::stringstre am sstr1(buf);
          std::cout << sstr1.str() << '\n';
          const std::string &temp(sstr1.str ());
          Why the reference here?
          std::for_each(t emp.begin(),tem p.end(),print);
          }
          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • kasthurirangan.balaji@gmail.com

            #6
            Re: Read memory buffer via stream interface

            On Mar 5, 6:49 pm, James Kanze <james.ka...@gm ail.comwrote:
            On Mar 5, 2:12 pm, kasthurirangan. bal...@gmail.co m wrote:
            >
            On Mar 5, 4:57 pm, Steven Woody <narkewo...@gma il.comwrote:
            >
                [...]
            >
            You may want to use this.
            >
            #include <iostream>
            #include <cstring>
            #include <sstream>
            #include <algorithm>
            #include <string>
            void print(const char &c)
            {
                    std::cout << std::showbase << std::hex << toascii(c) << ' ';
            >
            What's "toascii"?  The only "toascii" I know of is a very old
            pre-standard C Unix function---from the days when isupper, etc.
            only worked for ASCII characters.
            >
            yes, i know. may i know whats its replacement?
            (It's also considered good practice to restore the flags of an
            output stream after modifying them.  You don't necessarily want
            all of the output in the rest of the program to be in hex.)
            >
            definitely yes, thought OP might include that as part of his
            requirement.
            }
            main()
            >
            And of course, you'll need to declare main to return an int.
            >
            agreed.
            {
            std::stringstre am sstr;
            sstr << "balaji";
            std::cout << sstr.str() << '\n';
            sstr.str(""); //clear data
            char buf[100];
            strcpy(buf,"123 \n");
            std::stringstre am sstr1(buf);
            std::cout << sstr1.str() << '\n';
            const std::string &temp(sstr1.str ());
            >
            Why the reference here?
            >
            i thought of using sstr1.str().beg in() - which i am not sure whether
            it would work or not. By making const and reference(to a temporary), i
            went ahead with the below. If wrong, pls correct.
            std::for_each(t emp.begin(),tem p.end(),print);
            }
            >
            --
            James Kanze (GABI Software)             email:james.ka. ..@gmail.com
            Conseils en informatique orientée objet/
                               Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
            Thanks,
            Balaji.

            Comment

            • Steven Woody

              #7
              Re: Read memory buffer via stream interface

              On Mar 5, 9:12 pm, kasthurirangan. bal...@gmail.co m wrote:
              On Mar 5, 4:57 pm, Steven Woody <narkewo...@gma il.comwrote:
              >
              >
              >
              On Mar 5, 5:38 pm, kasthurirangan. bal...@gmail.co m wrote:
              >
              On Mar 5, 2:16 pm, Steven Woody <narkewo...@gma il.comwrote:
              >
              Hi,
              >
              for example, i have
              >
              unsigned char buff[ 100 ]
              >
              the buffer hold a sequences of raw bytes. My question is, what is the
              standard method of reading the `buff' via std::istream interface?
              >
              A relative question is: how to write to the `buff' via std::ostream
              interface? I think this can be done by using std::stringstre am, but i
              am not sure of it.
              >
              Thanks.
              >
              -
              woody
              >
              Use std::istringstr eam for input, std::ostringstr eam for output,
              std::stringstre am for both. Once you have created a stringstream, you
              can use istream/ostream read/write facilities on it.
              >
              #include <iostream>
              #include <cstring>
              #include <sstream>
              >
              main()
              {
              std::stringstre am sstr;
              sstr << "balaji";
              std::cout << sstr.str() << '\n';
              sstr.str(""); //clear in-memory data
              char buf[100];
              strcpy(buf,"asd kjabvdba");
              std::stringstre am sstr1(buf);
              std::cout << sstr1.str() << '\n';
              >
              }
              >
              Thanks,
              Balaji.
              >
              Thanks a lot! I just did not see that a stringstream can be
              constructed from a char[]. My book does not mentioned that. Where's
              a good STL reference I can find online or free?
              >
              Another question I also like to ask here:
              >
              After,
              char buf[100];
              strcpy(buf,"123 \n");
              std::stringstre am sstr1(buf);
              >
              How can I let cout << sstr1.str() << endl; prints something like that:
              >
              0x31 0x32 0x33 0x0a ?
              >
              Thanks again.
              >
              -
              woody- Hide quoted text -
              >
              - Show quoted text -
              >
              You may want to use this.
              >
              #include <iostream>
              #include <cstring>
              #include <sstream>
              #include <algorithm>
              #include <string>
              >
              void print(const char &c)
              {
              std::cout << std::showbase << std::hex << toascii(c) << ' ';
              >
              }
              >
              main()
              {
              std::stringstre am sstr;
              sstr << "balaji";
              std::cout << sstr.str() << '\n';
              sstr.str(""); //clear data
              char buf[100];
              strcpy(buf,"123 \n");
              std::stringstre am sstr1(buf);
              std::cout << sstr1.str() << '\n';
              const std::string &temp(sstr1.str ());
              std::for_each(t emp.begin(),tem p.end(),print);
              >
              }
              >
              You may also try for ebooks of C++ primer and The C++ Standard Library
              by Josuttis.
              >
              Thanks,
              Balaji.
              Thank you Balaji!

              Comment

              Working...