简体   繁体   English

使用makefile控制c++程序的output

[英]Control output of c++ program using makefile

I have an executable called print-word-length.cc that takes prints the length of words using from a *.txt file.我有一个名为 print-word-length.cc 的可执行文件,它使用 *.txt 文件打印单词的长度。

So in my makefile, ./print-word-length </dir/dictionary.txt prints out words in the dictionary.所以在我的 makefile 中,. ./print-word-length </dir/dictionary.txt打印出字典中的单词。

How would I go about just printing the first 10 lengths of words in dictionary.txt?我将如何 go 仅打印 dictionary.txt 中的前 10 个单词长度?

I've tried head -n 10./print-word-length </dir/dictionary.txt , but it prints gibberish.我试过head -n 10./print-word-length </dir/dictionary.txt ,但它打印出乱码。

Have you tried ./print-words.cc </dir/dictionary.txt | head -n 10你试过./print-words.cc </dir/dictionary.txt | head -n 10 ./print-words.cc </dir/dictionary.txt | head -n 10 ? ./print-words.cc </dir/dictionary.txt | head -n 10 This will print the first ten lines of the output.这将打印 output 的前十行。

This isn't a makefile question, it's a shell scripting question.这不是 makefile 问题,而是 shell 脚本问题。 You solve the problem by writing a shell command at your shell prompt, then you just put that same solution into your makefile.通过在 shell 提示符下编写 shell 命令来解决问题,然后将相同的解决方案放入 makefile 中。

The head utility prints the first lines it reads from standard input or from a file. head实用程序打印它从标准输入或文件中读取的第一行。 It doesn't run a command that you give it as an argument.它不会运行您将其作为参数提供的命令。

It's printing the first 10 lines of the contents of your program file, which is binary "gibberish" as you say.它正在打印程序文件内容的前 10 行,正如您所说,这是二进制“乱码”。

This is what pipes are for:这就是管道的用途:

./print-word-length.cc </dir/dictionary.txt | head -n10

This says: start print-word-length.cc and set it up so whatever it prints to its output is sent to the head command as its input.这表示:启动print-word-length.cc并对其进行设置,以便将打印到 output 的任何内容作为输入发送到head命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM