Book Image

Getting Started with Julia

By : Ivo Balbaert
Book Image

Getting Started with Julia

By: Ivo Balbaert

Overview of this book

Table of Contents (19 chapters)
Getting Started with Julia
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The Rationale for Julia
Index

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 d:\\test\\example.dat. The f1 variable is now an IOStream(<file example.dat>) object.

To read all lines one after the other in an array, use data = readlines(f1), which returns 3-element Array{Union...