0

假设我运行一个程序,该程序在其运行过程中生成多个 printf 语句。自然,每次它遇到 printf 命令时都会打印。我将如何进行而不是打印它,“存储”它,然后在程序结束时,取出所有应该打印的行,对其进行排序,然后打印它。

例如

Run Program
prints "File1 90"
prints "File2 30"
prints "File3 40"
End Program

Run Program
prints "File1 90" (don't actually print it out)
prints "File2 30" (don't actually print it out)
prints "File3 40" (don't actually print it out)
Take print statements and rearrange them by numerical order, then print
Program prints:
prints "File2 30"
prints "File3 40"
prints "File1 90"

我想我必须在我的 C 程序中使用 unix shell 命令,例如 sort -k2n,2 -k1,1 myprogram

4

1 回答 1

1

我能想到的一种方法是重定向stdout到文件

freopen( "file.txt", "w", stdout );

然后将文件提供给sort带有-k 2参数的文件,因为您想对第二列进行排序(?)

至于命令/s 参考这个例子

它说明了基础知识。

于 2013-01-24T02:49:32.390 回答