eof
eof(stream) -> Bool
Tests whether an I/O stream is at end-of-file. If the stream is not yet exhausted, this function will block to wait for more data if necessary, and then return false. Therefore it is always safe to read one byte after seeing eof return false. eof will return false as long as buffered data is still available, even if the remote end of a connection is closed.
Examples
julia> io = IOBuffer("Julia is awesome!")
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=18, maxsize=Inf, ptr=1, mark=-1)
julia> eof(io)
false
In this example, the eof function is used to check if an IOBuffer named io is at the end-of-file. Since the buffer still contains data, eof returns false.
julia> io = IOBuffer("Hello, World!")
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=13, maxsize=Inf, ptr=1, mark=-1)
julia> read(io, Char)
'H'
julia> read(io, Char)
'e'
julia> read(io, Char)
'l'
julia> read(io, Char)
'l'
julia> read(io, Char)
'o'
julia> eof(io)
false
In this example, characters are read from the IOBuffer io using the read function. After reading multiple characters, eof is called, and it still returns false because there is more data available in the buffer.
julia> io = IOBuffer("")
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)
julia> eof(io)
true
Here, eof is used to check if an empty IOBuffer named io is at the end-of-file. Since there is no data in the buffer, eof returns true.
julia> file = open("data.txt", "r")
IOStream(<file data.txt>)
julia> eof(file)
false
In this example, the eof function is used to check if an IOStream named file is at the end-of-file. Since there is more data available in the file, eof returns false.
julia> file = open("data.txt", "r")
IOStream(<file data.txt>)
julia> read(file, String)
"Hello, World!"
julia> eof(file)
true
In this example, data is read from the IOStream file using the read function. After reading the contents of the file, eof is called, and it returns true because there is no more data to read.
Common mistake example:
julia> io = IOBuffer("Julia is awesome!")
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=18, maxsize=Inf, ptr=1, mark=-1)
julia> eof(io)
true
In this example, the eof function is mistakenly used to check if there is more data available in the IOBuffer io. However, since the buffer still contains data, the correct output should be false.
See Also
deserialize, eachline, eof, fd, flush, IOBuffer, ismarked, isopen, isreadonly, mark, nb_available, open, pipeline, position, read, read!, readavailable, readbytes, readbytes!, readline, redirect_stderr, redirect_stdin, reset, seek, seekend, seekstart, serialize, skip, skipchars, TextDisplay, unmark, write, writemime,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.