'Read syscall in NASM does not stop after reaching to a newline character when the input is redirected from a file, not terminal

The following assembly code is supposed to read two separate strings in two different lines; and sure enough, it does work properly when the input is given by the user via terminal:

section .bss
    str1    resb    100
    str2    resb    100
    
section .text
    global _start
    
_start:
    ;Reading the first string
    mov rax,    3
    mov rbx,    0
    mov rcx,    str1
    mov rdx,    100
    int 0x80
    
    ;Reading the second string
    mov rax,    3
    mov rbx,    0
    mov rcx,    str2
    mov rdx,    100
    int 0x80

Exit:
    mov rax,    1
    mov rbx,    0
    int 0x80

To run this program (saved in a file named a.asm), I run the following commands in terminal:

nasm -f elf64 ./a.asm
ld -o ./a -e _start ./a.o
./a

It reads two strings in two lines and halts. But when I set an input file such as input.txt, it does not read the second string; the whole file content is read by the first read system call. The contents of input.txt:

Hello1
Hello2

To set the input file via terminal, I use the following command:

./a < input.txt

The problem, as I understand it, is that when the program is reading from a file, the read system call does not stop when it reaches the \n character and only stops when it reaches the NULL character at the end of the file: So reading only one string is not a problem.

I have tried replacing the \n character with \r, but did not work.

How can I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source