Reading a file line by line

question

#1

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.


#2
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"

#3

Is it really that hard to find? :slight_smile: 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

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


#4

this skipped my sight thanks :slight_smile: