Thursday, July 9, 2009

Eof() C++ problem.. maybe.. really i dont know where the problem is...?

I wrote the following program that calculate the nr of inversions:


#include%26lt;iostream%26gt;


#include%26lt;fstream%26gt;





using namespace std;





int main(){


fstream file("a.in");


if(file.is_open()){


int N;


int *arr;


while(!file.eof()){


file%26gt;%26gt;N; cout%26lt;%26lt;"N="%26lt;%26lt;N%26lt;%26lt;endl%26lt;%26lt;"ns are"%26lt;%26lt;endl;


arr=new int[N];


for(int i=0;i%26lt;N;i++){


file%26gt;%26gt;arr[i];


cout%26lt;%26lt;arr[i]%26lt;%26lt;" ";


}


cout%26lt;%26lt;endl%26lt;%26lt;"invs: "%26lt;%26lt;endl;


int n=0;


for(int i=0;i%26lt;N-1;i++){


for(int j=i+1;j%26lt;N;j++){


if(arr[i]%26gt;arr[j])


n++;


}


}


cout%26lt;%26lt;n%26lt;%26lt;endl%26lt;%26lt;endl;


}


file.close();


}


return 0;


}


It works fine but for one detail, the while loop executes itself once more than expected. for the following input file:


5


1 2 3 4 5


4


4 3 2 1


1


1000


10


9 7 5 2 3 4 10 1 6 8


i get the following output:


N=5


ns are


1 2 3 4 5


invs:


0





N=4


ns are


4 3 2 1


invs:


6





N=1


ns are


1000


invs:


0





N=10


ns are


9 7 5 2 3 4 10 1 6 8


invs:


24





N=10


ns are


0 0 0 0 0 0 0 0 0 0


invs:


0


I want to kow why. Plz help!!

Eof() C++ problem.. maybe.. really i dont know where the problem is...?
A google search would have revealed the answer, posted by other knowlegeable programmers. For example, one of the posts at http://www.thescripts.com/forum/thread13... reveals the problem with file.eof.





To understand what is happening, you need to see what eof does. http://www.cplusplus.com/reference/iostr...





Eof returns true when the eofbit is set. This statement should set off an alarm bell in your head. Eof doesn't return true when you have reached the end of the file. It return true when the eofbit is set. Notice the difference? You should be asking yourself, when is eofbit set?





It's set when an I/O operation fails because of EOF. This is a key piece of information. If you attempt to read from a file, and it is successful, clearly the operation hasn't failed. eofbit won't be set. What if you reached the end of the file after the operation? No matter. You tried to read from the file, it worked, and now you are at the end of the file. The last read worked, so obviously eofbit is not going to be set.





Now you try to read again, even though you are at the end of the file. The file read is unsuccessful. So N in your code doesn't change, because the I/O operation failed. Eofbit is now set, but look, it's a bit too late! You are already going through your loop.





So what can you do? You could try to read from the file. Then check if eofbit is set. And only then proceed with the rest of your code. However, you could always test the return value. See, the return value isn't just for decoration. It means something. when you file %26gt;%26gt; N, that operation returns a value, which you can test for success. Look at


http://www.fredosaurus.com/notes-cpp/io/...
Reply:you have a leaky faucet,its either very loose or too tight-
Reply:Your counting an extra loop iteration in the nested for loop. It repeats once more than it should. You can do it! :-)


No comments:

Post a Comment