read!

read!(stream, array::Array)

Read binary data from a stream, filling in the argument array.

Examples

  1. Read binary data from a stream into an array:

    julia> stream = IOBuffer([0x01, 0x02, 0x03, 0x04]);
    julia> array = zeros(UInt8, 4);
    julia> read!(stream, array)
    4-element Array{UInt8,1}:
    0x01
    0x02
    0x03
    0x04

    This example reads 4 bytes of binary data from the stream and fills the array with the read values.

  2. Read binary data from a file into an array:

    julia> file = open("data.bin", "r");
    julia> array = zeros(UInt16, 3);
    julia> read!(file, array)
    3-element Array{UInt16,1}:
    0x0102
    0x0304
    0x0506

    It reads 3 UInt16 values from a binary file (data.bin) and stores them in the array.

  3. Read binary data from a network socket into an array:
    julia> sock = connect("localhost", 8000);
    julia> array = zeros(Float64, 5);
    julia> read!(sock, array)
    5-element Array{Float64,1}:
    3.14
    2.71828
    1.414
    0.577
    2.236

    This example reads 5 Float64 values from a network socket (localhost:8000) and populates the array.

Common mistake example:

julia> stream = IOBuffer([0x01, 0x02, 0x03]);
julia> array = zeros(UInt8, 4);
julia> read!(stream, array)
ERROR: ReadError: attempted to read 4 bytes, but only got 3 bytes

In this example, the size of the array is larger than the available data in the stream. It's essential to ensure that the array size matches the amount of data to be read to avoid such errors.

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.

*Required Field
Details

Checking you are not a robot: