473,769 Members | 3,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with a diff algorithm

Hi all ! I'm currently writing a function that evaluate if there is a
diffence beetween 2 streams (istringstream and ifstream).

The problem is, it doesn't work. It *always* finds a difference at the
9th line because the first 8 lines are skipped, however, the is no
difference at all beetween the two streams, the only difference is that
after skipping the 8th first lines, one of the stream don't go forward
therefore a difference is found. Here's the function

//returns true if it find a difference
bool diff(string currzone, string zonefilename, ostream& verbose) {
istringstream iss(currzone); //The Current Zone File
ifstream ifs(zonefilenam e.c_str()); //The Old Zone File
string buff1, buff2;
//ignore the first 8 lines
for(int i=1; i<8; i++) {
getline(iss, buff1);
getline(ifs, buff2);
verbose << "Ignored from curr zone : " << buff1 << endl;
verbose << "ignored from old zone : " << buff2 <<endl;
}

while(true) {
if((getline(iss , buff1).eof() == true) && (getline(ifs,bu ff2).eof() == true)) {
//everythings ok
verbose << "Both file ends at same time, no diff" << endl;
return false;}

if(buff1 != buff2) {
//difference found
verbose << "Difference found :\n New Line : " << buff1
<< "\n Old Line : " << buff2 <<endl;
return true;
} else {
verbose << "Identic Lines : "<< endl << buff1 << endl << buff2 << endl;
}
}
//Shouldn't come up here
return true;

}

and the output produce when ostream& verbose = cout;

Ignored from curr zone : $ttl 38400
ignored from old zone : $ttl 38400
Ignored from curr zone : local. IN SOA enterprise. root.localhost. (
ignored from old zone : local. IN SOA enterprise. root.localhost. (
Ignored from curr zone : 2004211516 ;Serial Number
ignored from old zone : 2004211516 ;Serial Number
Ignored from curr zone : 10800 ; refresh
ignored from old zone : 10800 ; refresh
Ignored from curr zone : 3600 ; retry
ignored from old zone : 3600 ; retry
Ignored from curr zone : 604800 ; expire
ignored from old zone : 604800 ; expire
Ignored from curr zone : 3600 ) ; minimum
ignored from old zone : 3600 ) ; minimum
Difference found :
New Line : local. IN NS enterprise.
Old Line : 3600 ) ; minimum

We can notice the "old line" "forget" to go forward.. I really don't know
what's wrong

(thoses who works in DNS area will notice it's a BIND zone file..)

I really really don't know what's wrong and I really appreciate your help !
thanks a lot !

by the way.. English is not my main langage so excuse my errors.. thanks !
if it can help (that's why it's at the end since I'm not so shure it can
help but I just provides it) Here's the zone file produced by this program
(the same one that's read)

$ttl 38400
local. IN SOA enterprise. root.localhost. (
2004211516 ;Serial Number
10800 ; refresh
3600 ; retry
604800 ; expire
3600 ) ; minimum
local. IN NS enterprise.
josee.local. IN A 192.168.0.102
enterprise.loca l. IN A 192.168.0.101
deepspace9.loca l. IN A 192.168.0.100
Jul 22 '05 #1
4 2356
"Eric Boutin" <er***@nic.nac. wdyn.de> wrote in message
news:pa******** *************** *****@nic.nac.w dyn.de...
Hi all ! I'm currently writing a function that evaluate if there is a
diffence beetween 2 streams (istringstream and ifstream).

The problem is, it doesn't work. It *always* finds a difference at the
9th line because the first 8 lines are skipped, however, the is no
difference at all beetween the two streams, the only difference is that
after skipping the 8th first lines, one of the stream don't go forward
therefore a difference is found. Here's the function I'll try to point-out mistakes I see while reading on-the-fly
//returns true if it find a difference
bool diff(string currzone, string zonefilename, ostream& verbose) {
istringstream iss(currzone); //The Current Zone File
ifstream ifs(zonefilenam e.c_str()); //The Old Zone File
string buff1, buff2;
//ignore the first 8 lines
for(int i=1; i<8; i++) { Note that this loop will actually execute 7 times, not 8 !
Could this be the problem?
Try:
for( int i = 0 ; i<8 ; ++i ) getline(iss, buff1);
getline(ifs, buff2);
verbose << "Ignored from curr zone : " << buff1 << endl;
verbose << "ignored from old zone : " << buff2 <<endl;
}

while(true) {
if((getline(iss , buff1).eof() == true) && (getline(ifs,bu ff2).eof() ==
true)) {
//everythings ok
verbose << "Both file ends at same time, no diff" << endl;
return false;} The previous is not correct: a difference in the last line of both
files may be discarded ! (the eof() flag is set if line extraction
is successful but ends with hitting the end of the file).
Try:
bool ok1 = getline(iss,buf f1);
bool ok2 = getline(ifs,buf f2);
if( !ok1 && !ok2 ) return true;
if( ok1 != ok2 ) return false; // either file is shorter
// NB: print more detail if desired
if(buff1 != buff2) {
//difference found
verbose << "Difference found :\n New Line : " << buff1
<< "\n Old Line : " << buff2 <<endl;
return true;
} else {
verbose << "Identic Lines : "<< endl << buff1 << endl << buff2 <<
endl;
}
}
//Shouldn't come up here Then you may want to add something like: assert(false); return true;

}

I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2
fixed !
I based my algorithm on what you gave me and now it seems to work

thanks !
Jul 22 '05 #3
Eric Boutin wrote:
fixed !
I based my algorithm on what you gave me and now it seems to work

thanks !


The problem was as Ivan said. You should use fail (), not eof (), to
find out when you've finished reading a stream. It might be useful to
read the input/output section of the FAQ. People make fairly similar
errors quite often.

As for the strange behaviour (finding a difference in two identical
files), find out about short-circuit evaluation for the built-in &&
and || operators. It's not in the FAQ so use google.

if((getline(iss , buff1).eof() == true) && (getline(ifs,bu ff2).eof() ==
true)) {
// So far so good. We get here only if both getlines were
// executed and both streams now have eofbit set in rdstate().
// (That's not what we meant, but still ...)
} else {
// Trouble. If the first getline didn't set eofbit in iss.rdstate()
// then the second getline never got executed, so buff2 keeps its
// old value.
}

Either you can do what Ivan did and make sure both getlines are
executed every time round the loop, or you can arrange for the
short-circuit to kick in when there's a failure, rather than when
there's a success.

--
Regards,
Buster
Jul 22 '05 #4
Le Mon, 06 Dec 2004 02:01:00 +0000, Buster a écrit*:
As for the strange behaviour (finding a difference in two identical
files), find out about short-circuit evaluation for the built-in &&
and || operators. It's not in the FAQ so use google.


Thanks ! I really tought both instructions would be executed but I guess
I was wrong.. Thanks for your help !
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1589
by: erica | last post by:
Hi, I am having a very strange problem. I wrote a program in php 4.3.10 that uses objects. My classes.inc file looks like this: <?php // Coordinate class class Coord {
6
2168
by: Pete | last post by:
Hi Guys I have a form which must calc the difference between 2 date fields and return the result in a third field. I have the following code but it does not seem to work. Can anyone tell this total newbie where he is going wrong or suggest a more elegant way of doing this. thanks Pete <head><script>
4
2417
by: Gactimus | last post by:
Hey again, I am working my way through the book "C++ Primer Fourth Edition". I am trying to run a program out of the book using Microsoft Visual C++ version 6 and I am given the error message: : error C2248: 'hours' : cannot access private member declared in class 'Time' : error C2248: 'minutes' : cannot access private member declared in class 'Time'
9
6520
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the delta binary is pretty huge in size. I have 1 byte file and 2 bytes file, the delta should be 1 byte but somehow it turns out to be 249 bytes using binary formatter. I guess serialization has some other things added to the delta file.
4
4955
by: Andreas Kasparek | last post by:
Hola! I'm preparing my master thesis about a XML Merge Tool implementation and was wondering if there is any open standard for XML diff regarding topics like: - is a diff result computed on the ordered or unordered xml node tree of the compared documents? - what identifiers/criteria should be used by default to match elements of the same type in different documents? - should a diff tool consider move operations or only insert/delete
7
2559
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some calculations and stores elements in a list. After that, a sorting algorithm is used over that list. Two functions are called before the sorting process:
1
4539
by: duncan.welch | last post by:
Does anyone know of a good string diff algorithm they could point me in the direction of? I just need to display the differences between two reasonably large strings in a pretty format on a web page. Thanks in advance, Duncan
6
3317
by: Aaron Gray | last post by:
Hi, I am working on an HTML WYSISYG Wiki and need to display a diff page like WikiPedia does if two people edit a file at the same time to give the second user the diff. Basically with additions in red and deletions in red strike though. There seem to be several in Perl and Python and many diff programs which all seem to be line based and work on text written in PHP.
2
3348
by: presencia | last post by:
Hi everyone, I have written a small program running fine until a Segmentation fault. Using a debugger I have localized the function the fault happens in. The Signal appears during a call to delete in the recursive function I have posted below. I have done some C coding before but I am fairly new to C++. I would really appreciate any help. I can't figure out where the problem is. The function the SIGSEGV appears in is the following. It...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.