1

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. 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;
}
7
  • try this system(cmd1) Commented Aug 12, 2018 at 16:21
  • You have a line marked as "works". Is there still some problem with it? Commented Aug 12, 2018 at 16:23
  • using system(cmd1) gives me an error. Commented Aug 12, 2018 at 16:25
  • 1
    system(cmd1.c_str()) Commented Aug 12, 2018 at 16:26
  • 3
    signature is int system( const char * syscom );, not system(std::string) Commented Aug 12, 2018 at 16:29

3 Answers 3

2

The system function returns an integer which is the return value of the executed 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 ;)

Sign up to request clarification or add additional context in comments.

2 Comments

Don't forget to pclose(f) afterwards :)
@JeremyFriesner: Oh yeah! ;) I am lucky that my os will close it also if I exit, but you are rigt, it is bad as an example for a newbie! Thanks! Fixed!
0

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 .

Comments

0

use the wc console command to list the number of words in a specified file, after the user has entered the file's name. How can this be done?

On Linux, and with in my C++ programs, I use popen. Here is a snippet of code (part of class Metric_v05) which compiles.

// 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 (...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.