# FileIO - Minimal example

**URL:** <https://discourse.julialang.org/t/fileio-minimal-example/18097>\
**Category:** New to Julia\
**Created:** [November 28, 2018, 12:38pm UTC](https://discourse.julialang.org/t/fileio-minimal-example/18097 "2018-11-28T12:38:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [November 28, 2018, 12:38pm UTC](https://discourse.julialang.org/t/fileio-minimal-example/18097/1 "2018-11-28T12:38:52Z")

</div>

I am a newbie with julia and I am struggling a bit to understand FileIO.

I am trying to parse a file. I’d like to see a minimal working example. For example a module where I read text file (e.g. a .unv file and parse it line by line).

For instance, if we had a module call `Uff.jl`:

```julia
module Uff

using FileIO

# Should the format be registered here or when using the module
add_format(format"UFF", (), [".uff",".unv"])

# Streaming based reader
function load(s::Stream{format"UFF"}; keywords...)
    # s is already positioned after the magic bytes
    # Do the stuff to read a PNG file
    readline(s)

end

struct UFFReader
    io::IO
    ownstream::Bool
end

function Base.read(reader::UFFReader ) #, frames::Int)
    # read and decode audio samples from reader.io
    print(reader)
end

function Base.close(reader::UFFReader)
    # do whatever cleanup the reader needs
    reader.ownstream && close(reader.io)
end

loadstreaming(f::File{format"UFF"}) = UFFReader(open(f), true)
loadstreaming(s::Stream{format"UFF"}) = UFFReader(s, false)

# Reader based on the streaming
function load(q::Formatted{format"Uff"}, args...; kwargs...)
    loadstreaming(q, args...; kwargs...) do stream
        read(stream)
    end
end
 
end

```

So it can be used as suggested in the documentation:

```julia
using FileIO

add_format(format"UFF", (), [".uff",".unv"], [:Uff])

test = loadstreaming("example.unv")

try
    while !eof(audio)
        line = read(test)
    end
finally
    close(test)
end

```

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [November 28, 2018, 2:38pm UTC](https://discourse.julialang.org/t/fileio-minimal-example/18097/2 "2018-11-28T14:38:48Z")

</div>

The following seems to work.

I have created a module `Uff.jl`:

```julia
module Uff

using FileIO

struct UFFReader
    io::IO
    ownstream::Bool
end

function Base.read( reader::UFFReader )
    # read and decode audio samples from reader.io
    readline(reader.io)
end

function Base.close(reader::UFFReader)
    # do whatever cleanup the reader needs
    reader.ownstream && close(reader.io)
end

Base.eof(reader::UFFReader) = eof( reader.io )

loadstreaming(f::File{format"UFF"}) = UFFReader(open(f.filename), true)
loadstreaming(s::Stream{format"UFF"}) = UFFReader(s, false)

end

```

Just for the record it seems to work when I do:

```julia
using FileIO
using Uff

add_format(format"UFF", (), [".uff",".unv"], [:Uff])

loadstreaming("example.unv") do io
   while !eof(io)
     print(read(io))
   end
end

```

So it is a matter of replacing `readline(reader.io)` for whatever parser needs to be used and replacing `print(read(io))` for whatever processing we want to do in the future.

I will keep posting improvements as I learn them. (Just for the record)
