Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MessageBox

Streams vs. strings

This topic is 5478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve just started using streams and I was wondering what are the differences between the following variables: stringstream s1; strstream s2; ------------------------- I''m tired of using sprintf(), etc. all the time and these classes seem to to remove the need for the various s?() functions (most of the time). I also have tried using C++ strings as in and they also seem to be quite useful. So in addition to my first question, which is generally considered better to use: streams or C++ strings? Is there any point in using one exclusively or would using a combination of both be the ideal solution? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by MessageBox
I''ve just started using streams and I was wondering what are the differences
between the following variables:

stringstream s1;
strstream s2;

strstream is deprecated, stringstream isn''t. strstream pushes various memory management tasks onto the user which stringstream automates.
quote:

So in addition to my first question, which is generally considered better to use: streams or C++ strings?

You don''t use one or the other. They serve two different purposes, you choose whichever is suitable.

Share this post


Link to post
Share on other sites
Say I have a file formatted as follows:


John 10 1232.21
Joe 12 1233.33
Harry 34 1243.87


How would I be able to read that in efficiently using streams
So I could fill a class such as:


Class Person
{
public:
string name;
int age;
float cash;
}


BTW, I'm using VC++ 6 and I get the error message "fatal error C1083: Cannot open include file: 'stringstream': No such file or directory" when I include ...... Is my version of C++ outdated?




[edited by - MessageBox on September 23, 2003 7:10:46 AM]

Share this post


Link to post
Share on other sites
IIRC this should work, as streams eat whitespace:

ifstream fin("myfile.txt")
if (!fin) die slowly and painfully

fin >> p1.name >> p1.age >> p1.cash;
fin >> p2.name >> p2.age >> p2.cash;
fin >> p3.name >> p3.age >> p3.cash;

Writing that as a loop is left as an exercise for the reader.

Share this post


Link to post
Share on other sites
quote:
Original post by MessageBox
BTW, I''m using VC++ 6 and I get the error message "fatal error C1083: Cannot open include file: ''stringstream'': No such file or directory" when I include <stringstream>...... Is my version of C++ outdated?
It''s not hard to find what the correct header name is.

Share this post


Link to post
Share on other sites
I''ve included the header and it''s working fine now. Thanks

I was wondering how you check for failure and what to do in the
event of when using streams. I''ve read that you need to clear the stream using fin.clear(), so what happens then... Why would you want to continue if a stream failed?

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
IIRC this should work, as streams eat whitespace:

Streams don''t eat whitespace. Its the formatted input which is eating the whitespace.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by civguy
IIRC this should work, as streams eat whitespace:

ifstream fin("myfile.txt"
if (!fin) die slowly and painfully

fin >> p1.name >> p1.age >> p1.cash;
fin >> p2.name >> p2.age >> p2.cash;
fin >> p3.name >> p3.age >> p3.cash;

Writing that as a loop is left as an exercise for the reader.


fin.open("",ios::nocreate);

if (!fin.is_open())
you probably may want to use ios::nocreate as the file will be created if it doesn''t exist. and also ::is_open() instead of testing if the objet is null.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
fin.open("",ios::nocreate);

if (!fin.is_open())
you probably may want to use ios::nocreate as the file will be created if it doesn't exist. and also ::is_open() instead of testing if the objet is null.


1. No, look at ifstream::open()'s (and one of ifstream's overloaded constructor's) declarations:

(assuming the std namespace has been qualified - std::ios::in, etc.)

void open(const char* filename, openmode mode = in );
explicit ifstream(const char* filename, openmode mode = in);


2. Again, no. He is not "testing if the object is null", he is using std::ios::operator!(), whose behaviour is equivalent to that of std::ios::fail(). ios::fail()/ios::operator!() are better choices, as they check for if either the badbit or failbit stream state flags are set, i.e. any error other than EOF.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 23, 2003 11:29:54 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!