Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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++ 如何在c++;节目?_C++ - Fatal编程技术网

C++ 如何在c++;节目?

C++ 如何在c++;节目?,c++,C++,我正在做一个练习,要求在用户输入文件名后,使用wcconsole命令列出指定文件中的字数。如何做到这一点 以下是我目前管理的一些代码: int main() { string filename; string cmd1,cmd2,cmd3; cout<<"Please enter a filename: "; getline(cin, filename); cmd1="wc -l " +filename; //lines

我正在做一个练习,要求在用户输入文件名后,使用wcconsole命令列出指定文件中的字数。如何做到这一点

以下是我目前管理的一些代码:

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;
}
intmain()
{
字符串文件名;
字符串cmd1、cmd2、cmd3;

coutsystem
函数返回一个整数,该整数是已执行shell的返回值

您需要的是获得命令的输出,该输出可以通过以下方式完成:

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 ; 
}  
intmain()
{   
std::字符串文件名{“x.txt”};
字符串命令{“wc-l”};
命令+=文件名;
字符缓冲区[10000];
文件*f=popen(command.c_str(),“r”);
while(fgets(缓冲区,10000,f)!=nullptr)
{

这就是我要做的:

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;
std::string f;
cin>>f;
std::string c1=“wc-l”+f;
std::string c2=“wc-w”+f;
std::string c3=“wc-c”+f;
库特
使用wc console命令列出列表中的字数
指定的文件,在用户输入文件名后。如何
这能做到吗

在Linux上,在我的C++程序中,我使用POPON。这里有一个代码片段(MealCICIV05),编译。

// 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 (...)
//检索wc报告的内容
无效度量_v05::getRawLocCount(标准::字符串aPfn,
uint32\U t和线路计数,
uint32_t&wordCount,
uint32(字节数和字节数)
{
std::string cmd;
cmd=“wc”+aPfn;
//使用管道调用命令并检索结果
FILE*pipe=popen(cmd.c_str(),“r”);
做
{
如果(0==管道)断裂;//跳过pclose()
std::stringstream-ss;
{
char buff[1024];//假设所有这些行都比这个短(C-ism)
memset(buff,0,1024);
//从管道读取输出
如果(0==fgets(buff,1024,管道))
{
//当管道上没有更多输出时中断
std::cout>wordCount>>字节数;
//TBR-stat=ss>>xxx;如果(!stat)中断;
//我通常会推荐状态检查,
//但是wc是相当可靠的。
if(false)//诊断已禁用
std::cout try this
system(cmd1)
您有一行标记为“works”。它还有问题吗?使用system(cmd1)会给我一个错误。
system(cmd1.c_str())
签名是
int-system(const-char*syscom);
,而不是
system(std::string)
别忘了
pclose(f)
after:)@JeremyFriesner:哦,是的!;)我很幸运,如果我退出,我的操作系统也会关闭它,但你是对的,这对新手来说是个坏例子!谢谢!修复了!