运行 带有文件输入和文件输出的终端程序,输出大小有限制

Running Program in Terminal With File Input and File Output, with limits on Output size

我正在尝试 运行 终端中的可执行文件,使用 g++ 编译,具有单独的输入和输出 file/stream。但是我想限制输出,当输出文件达到特定的行数限制时,程序应该停止。我在bash.

看到了head命令的使用
./a.out | head --lines 100 <input.txt >output.txt

但是在执行时,它从 input.txt 文件获取输入,然后 t运行 分类 100 行并将它们打印到 output.txt 文件。但我想要它做的是,运行 a.out 可执行文件从 input.txt 文件获取输入,然后将结果打印到 output.txt 文件。我该如何完成?

But what I want it to do is, run the a.out executable taking input from input.txt file, and then printing the results to output.txt file.

重定向的正确用法是:

./a.out <input.txt | head --lines 100 >output.txt