# How to print the blank line in the file?

**URL:** <https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709>\
**Category:** General Usage\
**Created:** [November 16, 2024, 9:31am UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709 "2024-11-16T09:31:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 16, 2024, 9:31am UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709/1 "2024-11-16T09:31:37Z")

</div>

Hello,  
The input.txt file is as follows:

```julia
1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

```

The code below does not print the empty line:

```julia
inn = open("input.txt", "r")
inp = readline(inn)
while inp != ""
    println(inp)
    inp = readline(inn)
end

```

How to solve it?

Thank you.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 16, 2024, 9:52am UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709/3 "2024-11-16T09:52:47Z")

</div>

You could do:

```julia
input = readlines("input.txt")
for line in input
    println(line)
end

```

Does that solve your issue?

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [November 16, 2024, 10:01am UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709/4 "2024-11-16T10:01:37Z")

</div>

I’m pretty sure the ‘problem’ is that `readline` (with the default `keep=false`) gets rid of the newline characters, so that the second line, as stored in `inp`, is just `""`. Your `while` loop then stops, and you never run the `println` a second time. In particular, you do not print the empty line (or any line after that).

@ufechner7 's code does not have this issue, as the looping only stops when the input is completely exhausted.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 16, 2024, 12:14pm UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709/5 "2024-11-16T12:14:29Z")

</div>

Hello,  
I know that I can do this with the following code:

```julia
for line in eachline("input.txt")
    println(line)
end

```

But I am looking for a solution to fix my code problem.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 16, 2024, 12:15pm UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709/6 "2024-11-16T12:15:14Z")

</div>

Hello,  
How to fix the problem?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 16, 2024, 12:26pm UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709/7 "2024-11-16T12:26:00Z")

</div>

Well, if you call `readline` and the line is empty you get an empty string back. So there is no way to determine if the end of the file was reached. OK, you could use `readline(inn; keep=true)` and then remove the trailing linefeed character manually.

```julia
  readline(io::IO=stdin; keep::Bool=false)
  readline(filename::AbstractString; keep::Bool=false)

  Read a single line of text from the given I/O stream or file (defaults to
  stdin). When reading from a file, the text is assumed to be encoded in
  UTF-8. Lines in the input end with '\n' or "\r\n" or the end of an input
  stream. When keep is false (as it is by default), these trailing newline
  characters are removed from the line before it is returned. When keep is
  true, they are returned as part of the line.

```

But you did not really explain what you want to achieve and why you do not want to use `readlines()`, so it is difficult to help you.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 16, 2024, 12:58pm UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709/8 "2024-11-16T12:58:33Z")

</div>

> [@hack3rcon](#):
>
> But I am looking for a solution to fix my code problem.

is this ok for you?

```julia
while inp != "" || !eof(inn)
    println(inp)
    inp = readline(inn)
end

```

or this form

```julia
begin
inn = open("inp.txt", "r")

while !eof(inn) 
    inp = readline(inn)
    println(inp)
end
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 16, 2024, 2:42pm UTC](https://discourse.julialang.org/t/how-to-print-the-blank-line-in-the-file/122709/9 "2024-11-16T14:42:34Z")

</div>

> [@hack3rcon](#):
>
> `while inp != ""`

The problem with your code is that you are stopping when you encounter an empty line, rather than stopping at the end of the file.

Your halting condition should probably be `while !eof(inn)` instead, which checks that you haven’t reached the end of the file (eof).

And you should then check this _before_ calling `readline`, as otherwise you may not print the last line of the file. For example, similar to what @rocco_sprmnt21 posted above:

```julia
inn = open("input.txt", "r")
while !eof(inn)
    inp = readline(inn)
    println(inp)
end
close(inn)

```

As noted above, there are many other ways to do this, e.g. using `eachline` or `readlines`, but this is probably the closest to the spirit of how you are trying to write the code.
