Read input from text test file and print it to another text file

I decided to try coding in linux instead of windows and I have question . I currently taking course in C and when I have assignment I need to check the code with some tests from the course.

In windows I used to do something like that: First I opened cmd in the code file with the test.txt in it. Then I write this : hw5q1.exe<hw5q1in1.txt>hw5q1out1.txt and my program would print the output in hw5q1out1.txt file.

Is there a way I could do the same on linux ? I am using ubuntu and atom text editor.

2 answers

  • answered 2020-01-18 11:02 Alexander Falk

    You can cat the output of one file into the other.

    In your example, you can basically say:

    cat hw5q1in1.txt > hw5q1out1.txt
    

    Also, if you want the output from a program into a file (assuming it's writing to stdout), you can use the tee command (remember to compile the code to linux):

    ./hw5q1 | tee output.txt
    

    If you want to do it multiple times and overwrite the content in the output file use argument -a to overwrite:

    ./hw5q1 | tee -a output.txt
    

  • answered 2020-01-18 11:07 Joachim Astel

    I would write a small bash script:

    #!/usr/bin/env bash
    (read a; read b; cat hw5q1in1.txt) | tee hw5q1out1.txt
    

    tee duplicates the console output - to your terminal and to the file at the same time (line by line)