Solved

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

Posted on 2006-11-15
8
984 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does std::atomic give me? 7 124
java inner class, for the sole use of parameter passing 21 77
Safe conversion? 4 67
Which Linux flavors will this run on? 6 87
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. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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