448,703 Members | 1,184 Online
Need help? Post your question and get tips & solutions from a community of 448,703 IT Pros & Developers. It's quick & easy.

# Problem with a diff algorithm

 P: n/a 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(zonefilename.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 <
4 Replies

 P: n/a "Eric Boutin" wrote in message news:pa****************************@nic.nac.wdyn.d e... 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(zonefilename.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 <

 P: n/a fixed ! I based my algorithm on what you gave me and now it seems to work thanks ! Jul 22 '05 #3

 P: n/a 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,buff2).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

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.