# Print vs write

**URL:** <https://discourse.julialang.org/t/print-vs-write/123560>\
**Category:** New to Julia\
**Created:** [December 7, 2024, 9:12am UTC](https://discourse.julialang.org/t/print-vs-write/123560 "2024-12-07T09:12:29Z")\
**Posts on this page:** 6\
**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:** [December 7, 2024, 9:12am UTC](https://discourse.julialang.org/t/print-vs-write/123560/1 "2024-12-07T09:12:29Z")

</div>

Hello,  
I have a file like below:

```julia
Hello
Hello

```

I want to write a program that reads this file and writes its contents along with line numbers to another file. I wrote the following program:

```julia
function readwrite()
    inn = open("input.txt","r")
    out = open("output.txt","w")
    counter = 0
    while !eof(inn)
        r = readline(inn)
        counter +=1
        println(out, counter,": ",r)
    end
    close(inn)
    close(out)
end

readwrite()

```

This program works fine, but does not work with the following command:

```julia
write(out, counter,": ",r)

```

Why?

Thank you.

---

<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:** [December 7, 2024, 12:16pm UTC](https://discourse.julialang.org/t/print-vs-write/123560/2 "2024-12-07T12:16:11Z")

</div>

When I put the variable inside `" "`, then it works fine:

```julia
        write(out, "$counter :", r)

```

Why?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 7, 2024, 12:40pm UTC](https://discourse.julialang.org/t/print-vs-write/123560/3 "2024-12-07T12:40:18Z")

</div>

`write()` for a numeric value will output the raw bytes, while it preserves the strings encoding.

---

<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:** [December 8, 2024, 7:51am UTC](https://discourse.julialang.org/t/print-vs-write/123560/4 "2024-12-08T07:51:32Z")

</div>

Hi,  
Sorry, I can’t understand it.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 8, 2024, 8:33am UTC](https://discourse.julialang.org/t/print-vs-write/123560/5 "2024-12-08T08:33:24Z")

</div>

Check out the difference in this simple example:

```julia
write("test1.dat", 12)
write("test2.dat", "12")

read("test1.dat")
8-element Vector{UInt8}:
0x0c
0x00
0x00
0x00
0x00
0x00
0x00
0x00

read("test2.dat")
2-element Vector{UInt8}:
 0x31
 0x32

```

Decimal number 12 corresponds to hexadecimal letter C, and ASCII symbols 1 and 2 correspond to hexadecimals 31 and 32.

---

<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:** [December 8, 2024, 9:38am UTC](https://discourse.julialang.org/t/print-vs-write/123560/6 "2024-12-08T09:38:11Z")

</div>

> [@rafael.guerra](#):
>
> Decimal number 12 corresponds to hexadecimal letter C, and ASCII symbols 1 and 2 correspond to hexadecimals 31 and 32.

(It might be clearer to call C (or c) a ‘digit’ in this context, not to be confused with the character `'C'` with ASCII code 67 in decimal (0x43 hexadecimal) 🙂 )
