This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:fall2020:common:helpances:binaryfileandeof [2020/08/14 15:02] – wedge | haas: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/ | 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/ | ||
- | <code> | + | <blockquote> |
- | > I was having trouble finding EOF when reading from | + | 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(). |
- | > my input files last night. Neither the char or the | + | </blockquote> |
- | > unsigned char data types could seem to store EOF | + | |
- | > when I was reading it in with fgetc(). | + | |
- | > | + | |
- | </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 18: | Line 14: | ||
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 " | 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 " | ||
- | <code> | + | <blockquote> |
- | > Because of this my file reading loop check would | + | 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? |
- | > never fail and continued to read values after | + | </blockquote> |
- | > 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? | + | |
- | > | + | |
- | </code> | + | |
I suspect you are playing with the symbol EOF? As in: | I suspect you are playing with the symbol EOF? As in: | ||
Line 69: | 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, | + | * 00000000000000000000000011111111, |
- | significant byte comes last in actual memory representation, | + | |
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 |