# Reading a file line by line

**URL:** <https://discourse.julialang.org/t/reading-a-file-line-by-line/18256>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 3, 2018, 4:39pm UTC](https://discourse.julialang.org/t/reading-a-file-line-by-line/18256 "2018-12-03T16:39:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dhananjay\_Ghangale](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhananjay_ghangale/32/6249_2.png) [@Dhananjay\_Ghangale](https://discourse.julialang.org/u/Dhananjay_Ghangale)\
**Post date:** [December 3, 2018, 4:39pm UTC](https://discourse.julialang.org/t/reading-a-file-line-by-line/18256/1 "2018-12-03T16:39:14Z")

</div>

Hi guys I have a difficult in writing a function that can read file line by line. I have a .dat file which needs to be read line by line. I had written the function in matlab and while writting the function i was using fgetl, fscanf. I was trying to find the equivalents in julia but I am not very succesful. If anyone could point me to the functions that can allow me to do read the file line by line. Thanks.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 3, 2018, 4:42pm UTC](https://discourse.julialang.org/t/reading-a-file-line-by-line/18256/2 "2018-12-03T16:42:59Z")

</div>

```julia
help?> readlines
search: readlines readline readlink

  readlines(io::IO=stdin; keep::Bool=false)
  readlines(filename::AbstractString; keep::Bool=false)

  Read all lines of an I/O stream or a file as a vector of strings. Behavior is equivalent to saving the result of reading readline
  repeatedly with the same arguments and saving the resulting lines as a vector of strings.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> open("my_file.txt", "w") do io
             write(io, "JuliaLang is a GitHub organization.\nIt has many members.\n");
         end
  57
  
  julia> readlines("my_file.txt")
  2-element Array{String,1}:
   "JuliaLang is a GitHub organization."
   "It has many members."
  
  julia> readlines("my_file.txt", keep=true)
  2-element Array{String,1}:
   "JuliaLang is a GitHub organization.\n"
   "It has many members.\n"

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [December 3, 2018, 4:46pm UTC](https://discourse.julialang.org/t/reading-a-file-line-by-line/18256/3 "2018-12-03T16:46:15Z")

</div>

Is it really that hard to find? 🙂 Entering your question title in google and adding “Julia”: “Reading a file line by line in julia”, the first hit takes you here:

[https://en.wikibooks.org/wiki/Introducing\_Julia/Working\_with\_text\_files#Line\_by\_line](https://en.wikibooks.org/wiki/Introducing_Julia/Working_with_text_files#Line_by_line)

See also the manual here (note that you can use the file name directly in `eachline`):

[https://docs.julialang.org/en/v1/base/io-network/#Base.eachline](https://docs.julialang.org/en/v1/base/io-network/#Base.eachline)

---

<div class="post-metadata">

**Author:** ![Dhananjay\_Ghangale](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhananjay_ghangale/32/6249_2.png) [@Dhananjay\_Ghangale](https://discourse.julialang.org/u/Dhananjay_Ghangale)\
**Post date:** [December 3, 2018, 5:00pm UTC](https://discourse.julialang.org/t/reading-a-file-line-by-line/18256/4 "2018-12-03T17:00:32Z")

</div>

this skipped my sight thanks 🙂
