The code shown uses recursive I/O, which is prohibited. Quoting @sblionel from an Intel Forum thread about the same question,
An input/output statement that is executed while another input/output statement is being executed is a recursive input/output statement. A recursive input/output statement shall not identify an external unit that is identified by another input/output statement being executed except that a child data transfer statement may identify its parent data transfer statement external unit. (F2018 12.12p2)