c++ - Why does std::fstream set the EOF bit the way it does? -


I recently went into a problem using fstream :: eof (). I read the following line from the following:

function Aoff () Returns is correct if the related input has reached the end of the file, otherwise it is incorrect.

And (mistakenly) assumes that if I use fstream :: read () and before the end of the file, then the function will tell me (eof) I did something like this (very generalized):

 for  (int i = 0; i & lt; max & amp;; file.eof (); i ++) {file.read (Myfan, methane); }  

The problem has come up, which is mentioned later on the page given above (which I initially failed to read, because of the misleading first paragraph):

On the contrary, the stream does not go to the EOF state, if any white space has to be done after the last token, but trying to read another token will still fail, therefore, using the EOF flag Do not be used as a test in a loop Awareness is aimed to read all stream content to Iof. Instead, one should check the failed status after trying to read.

So I changed, and now how do I understand my loop check against file.file () instead of file.file () I work (my works) Why does the work? Are there conditions where it is desirable? It seems that once you have crossed the EOF, you have passed the EOF and the EOF () should be correct.

Thanks for the UPDATE responses, I understand that I have done it. The only operation I'm performing is read (), and I immediately (failed) to check, so I think I'm fine. Now, my question is, what is for eof ()?

In this way it can be detected that without the EOF The file is It's just an attempt to read and if the reading is low (but no error), then you have reached the end of the file.

This mirrors the functionality of system call, which usually ends calling the IO file (can call Win32 stuff to ReadFile But I believe the functionality is the same).

Read

and the file number is advanced by this number. This is not an error if this number is smaller than the number of requested bytes; This can be as an example because fewer bytes are actually available now (probably because we were close to concluding the file, or because we are studying with pipes, or from the terminal), or interrupted due to reading Was the signal -1 is returned on error, and errno is set appropriately, in this situation it is unspecified if the status of the file (if any) varies or not.

BTW: The one who wanted to write would be like this:

  T something; While (file.read (some, sizeof (some))) {/ / action on your 'something}}  

This works because file.read (Like many Iostream members) iostream has been overloaded for a reference back to all of which allow an operator to test the stream state. Similarly, std :: cin , works even from (std :: cin> x) {...} .

Edit: You should know that the test vs. failed may be equally incorrect for the same reason. Linked to previous page related to failed () when previous operation is unsuccessful. This means that you need to read or perform other relevant actions before testing.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -