Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取c++;来自awk输出的值 我在C++代码中使用了这行来计算Out.txt中的某些行。它打印正确的i值。现在我需要在C++中使用这个 > 计数值。有人能帮我吗。_C++_Awk - Fatal编程技术网

获取c++;来自awk输出的值 我在C++代码中使用了这行来计算Out.txt中的某些行。它打印正确的i值。现在我需要在C++中使用这个 > 计数值。有人能帮我吗。

获取c++;来自awk输出的值 我在C++代码中使用了这行来计算Out.txt中的某些行。它打印正确的i值。现在我需要在C++中使用这个 > 计数值。有人能帮我吗。,c++,awk,C++,Awk,你需要使用popen,而不是system 请看这里: 它就像fopen()和system()之间的交叉,它以unix风格的管道“返回”系统调用的输出,这很像一个文件*我想我的一部分就死在里面了。下面的C++代码将计算你的行数: system("awk 'BEGIN {i=0;}$0 ~/^D/ { i++; printf "i";}END {}' out.txt"); 这与wc-l输出相同: [wc-l $] ./count_lines test1 test2 test3 test4 fi

你需要使用
popen
,而不是
system

请看这里:
它就像fopen()和system()之间的交叉,它以unix风格的管道“返回”系统调用的输出,这很像一个文件*

我想我的一部分就死在里面了。下面的C++代码将计算你的行数:

system("awk 'BEGIN {i=0;}$0 ~/^D/ { i++; printf "i";}END {}' out.txt"); 
这与wc-l输出相同:

[wc-l $] ./count_lines test1 test2 test3 test4
file test1 contains 8 lines
file test2 contains 13 lines
file test3 contains 16 lines
unable to open file: test4
Total number of lines = 37
[wc-l $] 

请不要这样做。这是疯狂的谈话。用C++代码来做这件事有很多更快和相当简单的方法,而不是调用另一个外部解释程序,假设你在最后一节中不打印每一次都找到代码< i>代码>值,而不是每次找到匹配的时候…我还假设“<代码> <代码>周围<代码> > <代码>是不正确的。否则,如果‘我不是一个数字。无论如何,代码< > GRP-C“^”/代码>的价格也一样。但是我建议您考虑<代码> Salgar <代码>的注释。
[wc-l $] ./count_lines test1 test2 test3 test4
file test1 contains 8 lines
file test2 contains 13 lines
file test3 contains 16 lines
unable to open file: test4
Total number of lines = 37
[wc-l $] 
[wc-l $] wc -l test1 test2 test3 test4
 8 test1
13 test2
16 test3
wc: test4: No such file or directory
37 total
[wc-l $]