Solved

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

Posted on 2006-11-15
8
986 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
Technology Partners: 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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

679 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