如何在Ubuntu中使用C语言的命令终端

How to using command terminal in C in Ubuntu

本文关键字:语言 命令 终端 Ubuntu      更新时间:2023-10-16

我想在文件夹中复制一个文件,我想写一个函数来计算还有多少文件需要复制。

我可以用两个命令在终端中实现它。

第一个命令将获取文件夹中的所有文件-在处理

之前

find . -type f | wc -l => counter1 -原始文件数

第二个命令将获取当前文件夹中已经存在的所有文件。

find . -type f | wc -l => counter2 -原文件编号+复制文件

file_remaining = counter1 * 2 - counter2

counter1乘以2因为我想复制一个文件

如何在Ubuntu终端中使用C或c++编写一个函数来获取这两个命令的信息?

这就是我的答案。如果你不明白我的问题,请不要回答

#include <iostream>
#include<stdio.h>
using namespace std;
   int main( int argc, const char * argv[] )
   {      
         FILE * fp ;
         char tstCommand[] ="find . -type f | wc -l";
         char path[100];
         fp = popen(tstCommand, "r");
         while ( fgets( path, 100, fp ) != NULL )
               cout << path << endl;
         pclose(fp);
         return 0;
   }