Queries about strstream.h

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vishnu mahendra

    Queries about strstream.h

    hello to all,
    #include<iostre am.h>
    #include<conio. h>
    #include <strstream.h>
    void main()
    {
    clrscr();
    strstream str;
    char *a = new char[50];
    int i;
    cin >> i;


    ostrstream s;
    s<<"\nvalue of i is "<<i<<ends;
    cout<<"\nIn s we have"<<s.str();

    str<<i<<ends;
    cout<<"\nusing rdbuf "<<str.rdbu f();

    getch();
    }

    correct me if i am wrong.
    Is Strstream is just used for converting interger and formatting it into strings.

    what is the difference between "s.str()" and "str.rdbuf( )" ?
    the output is the same.

    Did strstream have anyother functions.
    I checked c++ library , but i cannot understand a word.


    thankyou in advance,
    vishnu
  • Buster

    #2
    Re: Queries about strstream.h

    vishnu mahendra wrote:

    Your textbook is due for recycling. Please don't sell it on.
    [color=blue]
    > #include<iostre am.h>[/color]

    Wrong. The standard C++ header you want is called 'iostream',
    without the ".h".
    [color=blue]
    > #include<conio. h>[/color]

    Wrong. There is no standard C++ header corresponding to this one.
    [color=blue]
    > #include <strstream.h>[/color]

    Wrong. This header and everything it provides is deprecated.
    You should use the 'sstream' header and the 'stringstream'
    class template instead.
    [color=blue]
    > void main()[/color]

    Wrong. The main function always returns int.
    [color=blue]
    > {
    > clrscr();[/color]

    Wrong. There is no way to clear the console's screen in standard C++.
    [color=blue]
    > strstream str;[/color]

    Should be "std::stringstr eam str;", but why bother, if you're not going
    to use it?
    [color=blue]
    > char *a = new char[50];[/color]

    (i). You never delete [] the memory allocated here. That's bad.
    (ii). You never use the variable a at all, so don't declare it.
    (iii). Why 50?

    Use std::string for strings.
    [color=blue]
    > int i;
    > cin >> i;[/color]

    Should be "std::cin >> i;".
    [color=blue]
    >
    > ostrstream s;[/color]

    Should be "std::ostringst ream s;"
    [color=blue]
    > s<<"\nvalue of i is "<<i<<ends;[/color]

    You don't need the "ends" with std::stringstre am,
    since std::string doesn't need to be null-terminated.
    [color=blue]
    > cout<<"\nIn s we have"<<s.str();[/color]

    OK, but qualify "cout" with "std::".
    [color=blue]
    > str<<i<<ends;[/color]

    What was that for?
    [color=blue]
    > cout<<"\nusing rdbuf "<<str.rdbu f();[/color]

    I daresay you don't need to know about std::basic_ios <>::rdbuf () yet.
    [color=blue]
    > getch();[/color]

    Wrong. There is no way to wait for a single keypress in standard C++.[color=blue]
    > }
    >
    > correct me if i am wrong.
    > Is Strstream is just used for converting interger and formatting it into strings.[/color]

    You shouldn't be using it at all. stringstream, on the other hand, is
    used for converting any built-in type into a string. It also works with
    user-defined types if you provide the right overloaded << operator.
    [color=blue]
    > what is the difference between "s.str()" and "str.rdbuf( )" ?
    > the output is the same.[/color]

    std::stringstre am::str () returns the string you want. std::basic_ios
    <>::rdbuf () returns a pointer to the streambuf used by the stream
    object on which it is called. The << operator is overloaded in the
    basic_ostream class to allow the insertion of a streambuf into an output
    stream. This is mostly of interest to people implementing stream, not
    users.
    [color=blue]
    > Did strstream have anyother functions.[/color]

    std::stringstre am has lots of member functions.
    [color=blue]
    > I checked c++ library , but i cannot understand a word.[/color]

    Get a better book. I recommend "The C++ Programming Language",
    3rd/Special edition, by Bjarne Stroustrup, for starters.
    [color=blue]
    > thankyou in advance,
    > vishnu[/color]

    --
    Regards,
    Buster.

    Comment

    • Buster

      #3
      Re: Queries about strstream.h

      vishnu mahendra wrote:

      Your textbook is due for recycling. Please don't sell it on.
      [color=blue]
      > #include<iostre am.h>[/color]

      Wrong. The standard C++ header you want is called 'iostream',
      without the ".h".
      [color=blue]
      > #include<conio. h>[/color]

      Wrong. There is no standard C++ header corresponding to this one.
      [color=blue]
      > #include <strstream.h>[/color]

      Wrong. This header and everything it provides is deprecated.
      You should use the 'sstream' header and the 'stringstream'
      class template instead.
      [color=blue]
      > void main()[/color]

      Wrong. The main function always returns int.
      [color=blue]
      > {
      > clrscr();[/color]

      Wrong. There is no way to clear the console's screen in standard C++.
      [color=blue]
      > strstream str;[/color]

      Should be "std::stringstr eam str;", but why bother, if you're not going
      to use it?
      [color=blue]
      > char *a = new char[50];[/color]

      (i). You never delete [] the memory allocated here. That's bad.
      (ii). You never use the variable a at all, so don't declare it.
      (iii). Why 50?

      Use std::string for strings.
      [color=blue]
      > int i;
      > cin >> i;[/color]

      Should be "std::cin >> i;".
      [color=blue]
      >
      > ostrstream s;[/color]

      Should be "std::ostringst ream s;"
      [color=blue]
      > s<<"\nvalue of i is "<<i<<ends;[/color]

      You don't need the "ends" with std::stringstre am,
      since std::string doesn't need to be null-terminated.
      [color=blue]
      > cout<<"\nIn s we have"<<s.str();[/color]

      OK, but qualify "cout" with "std::".
      [color=blue]
      > str<<i<<ends;[/color]

      What was that for?
      [color=blue]
      > cout<<"\nusing rdbuf "<<str.rdbu f();[/color]

      I daresay you don't need to know about std::basic_ios <>::rdbuf () yet.
      [color=blue]
      > getch();[/color]

      Wrong. There is no way to wait for a single keypress in standard C++.[color=blue]
      > }
      >
      > correct me if i am wrong.
      > Is Strstream is just used for converting interger and formatting it into strings.[/color]

      You shouldn't be using it at all. stringstream, on the other hand, is
      used for converting any built-in type into a string. It also works with
      user-defined types if you provide the right overloaded << operator.
      [color=blue]
      > what is the difference between "s.str()" and "str.rdbuf( )" ?
      > the output is the same.[/color]

      std::stringstre am::str () returns the string you want. std::basic_ios
      <>::rdbuf () returns a pointer to the streambuf used by the stream
      object on which it is called. The << operator is overloaded in the
      basic_ostream class to allow the insertion of a streambuf into an output
      stream. This is mostly of interest to people implementing stream, not
      users.
      [color=blue]
      > Did strstream have anyother functions.[/color]

      std::stringstre am has lots of member functions.
      [color=blue]
      > I checked c++ library , but i cannot understand a word.[/color]

      Get a better book. I recommend "The C++ Programming Language",
      3rd/Special edition, by Bjarne Stroustrup, for starters.
      [color=blue]
      > thankyou in advance,
      > vishnu[/color]

      --
      Regards,
      Buster.

      Comment

      • vishnu mahendra

        #4
        Re: Queries about strstream.h

        Hello sir,

        Thank you for the help. your help is much appreciated.
        By the way i forgot to tell you that i am using Turbo C++.
        i am a newbie so i made lots of mistakes.

        thankyou again,
        vishnu

        Comment

        • vishnu mahendra

          #5
          Re: Queries about strstream.h

          Hello sir,

          Thank you for the help. your help is much appreciated.
          By the way i forgot to tell you that i am using Turbo C++.
          i am a newbie so i made lots of mistakes.

          thankyou again,
          vishnu

          Comment

          • Buster

            #6
            Re: Queries about strstream.h

            vishnu mahendra wrote:
            [color=blue]
            > Thank you for the help. your help is much appreciated.
            > By the way i forgot to tell you that i am using Turbo C++.
            > i am a newbie so i made lots of mistakes.[/color]

            I'm glad you found it helpful. Sorry if I was rude.

            --
            Regards,
            Buster.

            Comment

            • Buster

              #7
              Re: Queries about strstream.h

              vishnu mahendra wrote:
              [color=blue]
              > Thank you for the help. your help is much appreciated.
              > By the way i forgot to tell you that i am using Turbo C++.
              > i am a newbie so i made lots of mistakes.[/color]

              I'm glad you found it helpful. Sorry if I was rude.

              --
              Regards,
              Buster.

              Comment

              Working...