Click here to Skip to main content
15,886,873 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Wordle 790 (2/6) Pin
Jeremy Falcon18-Aug-23 3:04
professionalJeremy Falcon18-Aug-23 3:04 
GeneralRe: Wordle 790 (4/6) Pin
musefan18-Aug-23 4:23
musefan18-Aug-23 4:23 
GeneralRe: Wordle 790 4/6 Pin
jmaida18-Aug-23 9:23
jmaida18-Aug-23 9:23 
Generala New Toy Pin
BernardIE531717-Aug-23 13:05
BernardIE531717-Aug-23 13:05 
JokeRe: a New Toy Pin
Daniel Pfeffer17-Aug-23 17:51
professionalDaniel Pfeffer17-Aug-23 17:51 
GeneralRe: a New Toy Pin
jmaida17-Aug-23 19:12
jmaida17-Aug-23 19:12 
GeneralRe: a New Toy Pin
BernardIE531717-Aug-23 19:14
BernardIE531717-Aug-23 19:14 
GeneralIrked! Irked I am. Pin
PIEBALDconsult17-Aug-23 10:27
mvePIEBALDconsult17-Aug-23 10:27 
Having nothing better to do at the moment, I'm re-writing some of my file-reading code.
Previously, I would wrap a FileStream in a StreamReader and call it good -- that's good enough for many simple tasks, but it has some issues when used with some more difficult tasks.
I'm still using a FileStream, but now I'm wrapping it in a family of classes which add some features and eliminate some shortcomings.

What irks me -- and has for a while -- is how FileStream.ReadByte() reports END-OF-FILE:
"The byte, cast to an Int32, or -1 if the end of the stream has been reached."
I do realize that this matches the behavior of C's fgetc function.
Basically, it means that you have to test every return value for -1 (EOF) -- YUCK! so inefficient.
But this isn't C, I would much rather have FileStream.ReadByte() throw an Exception when it hits EOF.

Assuming that FileStream.ReadByte() uses the ReadFile Windows API function, I'm sure that the FileStream class could have a method with the ability to raise an Exception when it hits the EOF -- allow the user to choose how EOF is reported.

And, no, testing for EOF and then throwing is not an option, as it doesn't remove the test, which is the whole issue.

So do I now need to look into writing my own version of FileStream and have it throw an Exception?


Note:
I read a blog post about file handling improvements in .net 6, but it didn't seem to address this.
It should be easy enough for them to add a ThrowOnEOF option.


P.S. File length is reported in bytes, not characters, so comparing file length to characters read (as you get with StreamReader when reading a file which contains multi-byte characters) is not a solution.
And also, in some cases (hopefully rare today), a file may be logically terminated with char 26 (Ctrl-Z) even if the file size in bytes is reported as larger. To support that, a reader must still test every character returned.
Edit: I just read the following on a Wikipedia page about Unicode:
"Control-D has been used to signal "end of file" for text typed in at the terminal on Unix / Linux systems. Windows, DOS, and older minicomputers used Control-Z for this purpose."
"Control-Z has commonly been used on minicomputers, Windows and DOS systems to indicate "end of file" either on a terminal or in a text file. Unix / Linux systems use Control-D to indicate end-of-file at a terminal."

modified 19-Aug-23 0:06am.

GeneralRe: Irked! Irked I am. Pin
BernardIE531717-Aug-23 12:55
BernardIE531717-Aug-23 12:55 
GeneralRe: Irked! Irked I am. Pin
PIEBALDconsult17-Aug-23 13:40
mvePIEBALDconsult17-Aug-23 13:40 
GeneralRe: Irked! Irked I am. Pin
BernardIE531717-Aug-23 13:55
BernardIE531717-Aug-23 13:55 
GeneralRe: Irked! Irked I am. Pin
Peter_in_278017-Aug-23 14:59
professionalPeter_in_278017-Aug-23 14:59 
GeneralRe: Irked! Irked I am. Pin
BernardIE531717-Aug-23 15:41
BernardIE531717-Aug-23 15:41 
GeneralRe: Irked! Irked I am. Pin
PIEBALDconsult17-Aug-23 15:40
mvePIEBALDconsult17-Aug-23 15:40 
GeneralRe: Irked! Irked I am. Pin
Graeme_Grant17-Aug-23 16:18
mvaGraeme_Grant17-Aug-23 16:18 
GeneralRe: Irked! Irked I am. Pin
PIEBALDconsult17-Aug-23 16:31
mvePIEBALDconsult17-Aug-23 16:31 
GeneralRe: Irked! Irked I am. Pin
Graeme_Grant17-Aug-23 16:49
mvaGraeme_Grant17-Aug-23 16:49 
GeneralRe: Irked! Irked I am. Pin
Daniel Pfeffer17-Aug-23 17:41
professionalDaniel Pfeffer17-Aug-23 17:41 
GeneralRe: Irked! Irked I am. Pin
Richard Deeming17-Aug-23 21:33
mveRichard Deeming17-Aug-23 21:33 
GeneralRe: Irked! Irked I am. Pin
Jörgen Andersson17-Aug-23 21:40
professionalJörgen Andersson17-Aug-23 21:40 
GeneralRe: Irked! Irked I am. Pin
PIEBALDconsult18-Aug-23 4:13
mvePIEBALDconsult18-Aug-23 4:13 
GeneralRe: Irked! Irked I am. Pin
Jörgen Andersson18-Aug-23 4:35
professionalJörgen Andersson18-Aug-23 4:35 
GeneralRe: Irked! Irked I am. Pin
PIEBALDconsult18-Aug-23 5:52
mvePIEBALDconsult18-Aug-23 5:52 
GeneralRe: Irked! Irked I am. Pin
jschell18-Aug-23 12:15
jschell18-Aug-23 12:15 
GeneralRe: Irked! Irked I am. Pin
PIEBALDconsult18-Aug-23 12:51
mvePIEBALDconsult18-Aug-23 12:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.