Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1004
  • Last Modified:

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

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
jdpipe
Asked:
jdpipe
  • 5
1 Solution
 
rajeev_devinCommented:
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
 
rajeev_devinCommented:
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
 
jdpipeAuthor Commented:
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.

 
rajeev_devinCommented:
>> 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
 
rajeev_devinCommented:
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
 
rajeev_devinCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now