Working with files

To work with files, we need the IOStream type. IOStream is a type with the supertype IO and has the following characteristics:

  • The fields are given by names(IOStream)
    4-element Array{Symbol,1}:  :handle   :ios    :name   :mark
  • The types are given by IOStream.types
    (Ptr{None}, Array{Uint8,1}, String, Int64)

The file handle is a pointer of the type Ptr, which is a reference to the file object.

Opening and reading a line-oriented file with the name example.dat is very easy:

// code in Chapter 8\io.jl
fname = "example.dat"                                 
f1 = open(fname)

fname is a string that contains the path to the file, using escaping of special characters with \ when necessary; for example, in Windows, when the file is in the test folder on the D: drive, this would become ...

Get Getting Started with Julia now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.