Solved

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

Posted on 2006-11-15
8
990 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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.

617 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