简体   繁体   English

如何在C ++程序中简单地使用控制台命令?

[英]How to simply use console commands in a c++ program?

I am doing an exercise that requires me to use the wc console command to list the number of words in a specified file, after the user has entered the file's name. 我正在做一个练习,要求用户输入文件名后,使用wc console命令列出指定文件中的单词数。 How can this be done? 如何才能做到这一点?

Here is some code I'v managed so far: 这是到目前为止我管理的一些代码:

int main()
{
    string filename;
    string cmd1,cmd2,cmd3;


    cout<<"Please enter a filename: ";
    getline(cin, filename);


    cmd1="wc -l " +filename;        //lines
    cmd2="wc -w " + filename;   //words
    cmd3="wc -c "+filename;   //bytes
    /

    cout<<system("\\cmd1\\")<<endl;   //does not work

    cout<<system("wc -l device_data.txt")<<endl;   //works


    return 0;
}

The system function returns an integer which is the return value of the executed shell. system函数返回一个整数,该整数是已执行shell的返回值。

What you want is to get the output of your command which can be done with: 您想要得到的命令输出可以通过以下方式完成:

int main()
{   
    std::string filename{"x.txt"};
    std::string command{"wc -l "};
    command+=filename;

    char buffer[10000];
    FILE *f = popen(command.c_str(), "r");
    while ( fgets(buffer, 10000, f) != nullptr )
    {
        std::cout << buffer << std::endl;
    }

    pclose( f );
    return 0 ; 
}  

You should read the man pages before typing the source code ;) 在键入源代码之前,您应该阅读手册页;)

This is what i would do : 这就是我会做的:

std::string f;
cin>>f;

std::string c1 = "wc -l " + f;
std::string c2 = "wc -w " + f;
std::string c3 = "wc -c " + f;

cout<<system(c1.c_str())<<endl;
cout<<system(c2.c_str())<<endl;
cout<<system(c3.c_str())<<endl;

Here the thing is system takes char* as parameter and you were using string datatype . 这里的事情是系统将char *作为参数,而您正在使用字符串数据类型。

use the wc console command to list the number of words in a specified file, after the user has entered the file's name. 用户输入文件名后,使用wc console命令列出指定文件中的单词数。 How can this be done? 如何才能做到这一点?

On Linux, and with in my C++ programs, I use popen. 在Linux上以及在我的C ++程序中,我使用popen。 Here is a snippet of code (part of class Metric_v05) which compiles. 这是一个可编译的代码片段(属于Metric_v05类)。

// retrieve what wc reports
void Metric_v05::getRawLocCount (std::string aPfn,
                                 uint32_t&   lineCount,
                                 uint32_t&   wordCount,
                                 uint32_t&   byteCount)
{
   std::string cmd;

   cmd = "wc " + aPfn;

   // use pipe to invoke the command and retrieve results
   FILE* pipe = popen(cmd.c_str(), "r");

   do
   {
      if(0 == pipe) break; // skip over pclose()

      std::stringstream ss;
      {
         char buff[1024]; // assume all these lines will be shorter than this (C-ism)
         memset(buff, 0, 1024);

         // read output from pipe
         if (0 == fgets(buff, 1024, pipe))
         {
            // break out when no more output at pipe
            std::cout << "line word byte ?" << std::endl;
            pclose(pipe);
            break; // queue complete, eof, kick out
         }
         ss << buff;
      }

      // output of wc / input to the buff is "line word byte", such as:
      //  1257    4546   44886

      ss >> lineCount >> wordCount >> byteCount;
      // TBR - stat = ss >> xxx; if(!stat) break;
      // I would normally recommend status check,
      // but wc is quite reliable.

      if (false)  // diagnostic disabled
         std::cout << "lwb: "
                   << lineCount << " "
                   << wordCount << " "
                   << byteCount << std::endl;

      // be sure to
      pclose(pipe);

   } while (0); // only 1 line to fetch, drop any others.

} // void Metric_v05::getRawLocCount (...)

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

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