alternate to string streams

This is a discussion on alternate to string streams within the C++ Programming forums, part of the General Programming Boards category; hi, just looking for an alternative to using <strstream.h>, using msvc++ 9 exp and it comes missing almost every header ...

  1. #1
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80

    alternate to string streams

    hi, just looking for an alternative to using <strstream.h>,
    using msvc++ 9 exp and it comes missing almost every header that i need , i have installed most of them bar this one, was just wondering aside from using this header what is avaliable to me, heres what i want to do as an example:

    Code:
    DWORD longNumber=GetTickCount();
    char Data[]=" Data: ";
    strstream myString;
    myString<<"Some"<<Data<<longNumber<<ends;
    Last edited by MicroFiend; 04-29-2008 at 01:12 PM. Reason: typos
    You can stop change as easily as u can drink the sea with a fork

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The alternative to strstream is called a stringstream and is in the header <sstream>.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's something I've been wondering for a while too.
    Why is there both a strstream and a stringstream class?
    Was strstream ever officially part of the C++ standard, or is it kind of like those <conio.h> functions that only exist on some compilers?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I believe that strstream was pre-standard, then included but immediately deprecated when the standard was created for C++.

  5. #5
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80
    cheers for that.. it is a bit anoying when they change things like that altho im sure i shlda dropped using c++ 6 ages ago

    im having one more problem now,

    i used to be able to do:
    Code:
    strcpy(char array,strstream.str());
    but i find a whole heap of failed conversion warnings being thrown at me, i need to convert the string data held by the stream so that i can output the resulting char array to a file using
    Code:
    fputs();
    have things changed so much that i can no longer do things this way...
    Last edited by MicroFiend; 04-29-2008 at 02:27 PM. Reason: typos
    You can stop change as easily as u can drink the sea with a fork

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    Perhaps
    Code:
    #include <cstdio>
    #include <sstream>
    #include <string>
    #include <windows.h> // for GetTickCount, DWORD
    
    using namespace std; 
    
    // ...
    
    stringstream myString;
    DWORD longData = GetTickCount();
    myString << "Data: " << longData << '\n';
    fputs( stdout, myString.str().c_str() );

  7. #7
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80
    Code:
    .c_str()
    most appreciated works like a charm
    You can stop change as easily as u can drink the sea with a fork

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    But you might want to get away from all the C stuff anyway. For example, instead of
    Code:
    fputs(stdout, myString.str().c_str() );
    you could write
    Code:
    #include <iostream>
    
    // ...
    
    cout << myString.str() << '\n';
    (Not sure if fputs() appends a newline.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    #include <iostream>
    #include <string>
    
    // ...
    
    cout << myString.str() << '\n';
    Minor point but it's commonly missed by new-to-C++ folks.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I didn't include it because it was already present in citizen's example, but you're right of course. It never hurts to be explicit.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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