"Eric Boutin" <ericb@nic.nac.wdyn.de> wrote in message
news:pan.2004.12.05.21.02.07.503790@nic.nac.wdyn.d e...[color=blue]
> 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[/color]
I'll try to point-out mistakes I see while reading on-the-fly
[color=blue]
> //returns true if it find a difference
> bool diff(string currzone, string zonefilename, ostream& verbose) {
> istringstream iss(currzone); //The Current Zone File
> ifstream ifs(zonefilename.c_str()); //The Old Zone File
> string buff1, buff2;
> //ignore the first 8 lines
> for(int i=1; i<8; i++) {[/color]
Note that this loop will actually execute 7 times, not 8 !
Could this be the problem?
Try:
for( int i = 0 ; i<8 ; ++i )[color=blue]
> 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,buff2).eof() ==
> true)) {
> //everythings ok
> verbose << "Both file ends at same time, no diff" << endl;
> return false;}[/color]
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,buff1);
bool ok2 = getline(ifs,buff2);
if( !ok1 && !ok2 ) return true;
if( ok1 != ok2 ) return false; // either file is shorter
// NB: print more detail if desired
[color=blue]
> 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[/color]
Then you may want to add something like: assert(false);[color=blue]
> return true;
>
> }[/color]
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form