Solved

Compare contents of two istreams using STL (C++)

Posted on 2006-11-15
8
982 Views
Last Modified: 2008-01-09
Hi all

Can anybody give me a quick code snippet to compare the contents of two istreams?

In particular, I would like to be able to use this code to compare a stringstream with an ifstream, but hopefully it will generalise to pairs of istreams in general?

Code should work with MinGW and Linux (GCC). The application is for testing some routines that makes ostream output; I want to check the expected output against the real output. I'm just not sure what the preferred and efficent approach here would be, in line with STL principles.

Thanks for any ideas!

Cheers
JP
0
Comment
Question by:jdpipe
  • 5
8 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17953894
You can do like this

bool isEqual(istream & in1, istream & in2)
{
      char ch1, ch2;
      while (in1.get(ch1))
      {
            if (!in2.get(ch2) || (ch1 != ch2))
                  return (false);
      }
      if (!in2.get(ch2))
            return (true);
      else
            return (false);

      return (false);
}
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17953899
That function can be used in this way.

ifstream fs("test.txt"); // test.txt file sould also contain "This is a test string." to be equal. Else it will return false.
stringstream ss;
ss << "This is a test string.";

bool equal = isEqual(fs, ss);
0
 
LVL 7

Author Comment

by:jdpipe
ID: 17953949
Hi Rajeev,

I'm concerned that the approach you suggested might be sensitive to line endings on different platforms. Would that be true?

What about a line-by-line approach?

Cheers
JP
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17953959
>> I'm concerned that the approach you suggested might be sensitive to line endings on different platforms. Would that
>> be true?
That won't be a problem.
Since in C++ streams line ending is considered as '\n'. Whatever be the platform.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17953978
You can try with line comparison. like this.

char ch1[2048]; // you need a char array.
in1.getline(ch1, 2048);

But with line comparison you need to do more error checking as you are not sure about the line size.
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 200 total points
ID: 17954284
For line-by-line comparison you can use this.

bool isEqual(istream & in1, istream & in2)
{
      string str1, str2;

      do
      {
            getline(in1, str1);
            getline(in2, str2);

            if (str1 != str2)
                  return (false);

            if (in1.eof() && !in2.eof())
                  return (false);

            if (!in1.eof() && in2.eof())
                  return (false);

      }while(!in1.eof() && !in2.eof());

      return (true);
}
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now