SIO
SIO

Reputation: 305

passing variable (of char) to popen in C++

I am trying to pass a string of characters to popen from user input and grep command to retrieve the first element in a specified folder the user indicates.

I have looked around, but have not been able to find a good example. This is the code I have;

FILE *fp = popen("grep -n ", term, " ", file_to_search, " | cut -f 1" );    
//  This is a note: <---- term (changing variable) and 
//  file_to_search (stagnant variable indicated by the user)
char buf[1024];

while (fgets(buf, 1024, fp)) {
    cout << buf << endl ;       // This is a note: <------- is this right to call the return info? 
}
fclose(fp);

Upvotes: 0

Views: 2288

Answers (2)

SIO
SIO

Reputation: 305

I ended up writing a script to do this, but would still want to pursue and find an answer to get a c++ version up and running.

int main () {

ifstream file ;
string line ;
string input, file_to_search ;

cout << "File you want to open in the directory: " ;
cin >> input ;

cout << endl << "File you want to search against: " ;
cin >> file_to_search ;

file.open( input.c_str()) ;
vector <string> file_content ;

if (file.is_open()) {

    while (getline(file, line)) {
        file_content.push_back(line) ;
    }

    vector <string> :: iterator p ;

    for (p = file_content.begin( ); p != file_content.end( ); p++) {
        cout << *p << endl ;

        string term = *p ;







        char command[10000000000000] ;
        sprintf(command, "grep -n '%s' '%s' | cut -f 1", term.c_str(), file_to_search.c_str()) ;

        FILE *fp = popen(command, "r") ;
        if (fp == NULL ) {
            cout << "Error. " << endl ;
        } else {
            cout << "Command output: " << fp << endl ;
        }


        /*
        char command[10000000000000000]; // Make it large enough.
        sprintf(command, "grep -n '%s' '%s' | cut -f 1", term, file_to_search);
        //  Use of '' around term and file_to_search allows you to have
        //  whitespaces in them.

        FILE *fp = popen(command, "r");
        if ( fp == NULL )
        {
            // Deal with error condition.
        }

        // Rest of your code.

        */










    }

    string program_abort ;
    cout << endl << endl << "Does the system input look right ( y or n): " << endl ;
    cin >> program_abort ;

    if ((program_abort == "yes") || (program_abort == "y") ) {

        cout << endl << "Program will continue. " << endl << endl ;

    } else if ((program_abort == "no") || (program_abort == "n") || (program_abort != "no") ) {

        cout << endl << "Program has closed. " << endl << endl ;

        return 0 ;
    }
    file.close();






}
return 0 ;

}

Upvotes: 0

R Sahu
R Sahu

Reputation: 206577

Your attempt to use popen is off quite a bit from what the function expects. See the man pages.

You need something along the lines of:

char command[1000]; // Make it large enough.
sprintf(command, "grep -n '%s' '%s' | cut -f 1", term, file_to_search);
//  Use of '' around term and file_to_search allows you to have 
//  whitespaces in them.

FILE *fp = popen(command, "r");
if ( fp == NULL )
{
    // Deal with error condition.
}

// Rest of your code.

Upvotes: 2

Related Questions