User Tools

Site Tools


haas:fall2020:common:helpances:binaryfileandeof

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
haas:fall2020:common:helpances:binaryfileandeof [2020/08/14 14:54] – created wedgehaas:fall2020:common:helpances:binaryfileandeof [2020/08/14 21:43] (current) wedge
Line 3: Line 3:
 Here is a copy of a reply I had to some questions I suspect many of you may be encountering when dealing with certain file aspects of discrete/bdt0: Here is a copy of a reply I had to some questions I suspect many of you may be encountering when dealing with certain file aspects of discrete/bdt0:
  
-<code+<blockquote
-I was having trouble finding EOF when reading from my input files last night. Neither the char +I was having trouble finding EOF when reading from my input files last night. Neither the char or the unsigned char data types could seem to store EOF when I was reading it in with fgetc(). 
-or the unsigned char data types could seem to store EOF when I was reading it in with fgetc(). +</blockquote>
-> +
-</code>+
  
 Yeah, you may notice that **fgetc(3)** will seem to read in a **0xFF** as it encounters the **EOF**. The trick Yeah, you may notice that **fgetc(3)** will seem to read in a **0xFF** as it encounters the **EOF**. The trick
Line 14: Line 12:
  
 My preferred approach to this is to use **fgetc(3)** in combination with **feof(3)** -- it operates more My preferred approach to this is to use **fgetc(3)** in combination with **feof(3)** -- it operates more
-on the notion of some underlying flag being set when the EOF is encountered. This way, I don't +on the notion of some underlying flag being set when the EOF is encountered. This way, I don't have to bother interpreting whether this 0xFF is the critical "droids I'm looking for", I let the stdio subsystem do it for me.
-have to bother interpreting whether this 0xFF is the critical "droids I'm looking for", I let the +
-stdio subsystem do it for me.+
  
-<code+<blockquote
-Because of this my file reading loop check would never fail and continued to read values after +Because of this my file reading loop check would never fail and continued to read values after the end of the file was reached. But once I changed the variable to an integer it recognized EOF and the check utility lit up nicely. Why is this? 
-the end of the file was reached. But once I changed the variable to an integer it recognized EOF +</blockquote>
-and the check utility lit up nicely. Why is this? +
-> +
-</code>+
  
 I suspect you are playing with the symbol EOF? As in: I suspect you are playing with the symbol EOF? As in:
Line 67: Line 60:
 Taking our char's -1 and putting it in an int's point of view, we'd have: Taking our char's -1 and putting it in an int's point of view, we'd have:
  
-  * 00000000000000000000000011111111, which a signed int would likely see as +255 (assuming least +  * 00000000000000000000000011111111, which a signed int would likely see as +255 (assuming least significant byte comes last in actual memory representation, regardless of endianness).
-significant byte comes last in actual memory representation, regardless of endianness).+
  
 If so, (char)-1 is NOT the same binary value as (int)-1, and if EOF is (int)-1, the best we can do If so, (char)-1 is NOT the same binary value as (int)-1, and if EOF is (int)-1, the best we can do
haas/fall2020/common/helpances/binaryfileandeof.1597416848.txt.gz · Last modified: 2020/08/14 14:54 by wedge