Note that there are some explanatory texts on larger screens.

plurals
  1. POStay in directory with popen
    text
    copied!<p>I want to make some C++ program and I'm using function <code>popen</code> here to send commands to command line in Unix. It works fine, but when I call <code>cd directory</code>, the directory doesn't change. I thing that it's same when I try to run <code>cd directory</code> in some script, after finishing script directory path change back. So, scripts I must run like <code>. ./script.sh</code> not <code>./sript.sh</code>, but how to do that with <code>popen</code> function? I have tried to add <code>". "</code> before first argument of <code>popen</code>, but running <code>". ls"</code> makes error.</p> <p><strong>Code:</strong></p> <pre><code>cout &lt;&lt; "@ Command from " &lt;&lt; session-&gt;target().full() &lt;&lt; ": " &lt;&lt; message.body() &lt;&lt; endl; //cout &lt;&lt; "Prisla zprava" &lt;&lt; endl; //m_session-&gt;send( "Hello World", "No Subject" ); //system( message.body().c_str() ); //if ( message.body() == "" ) FILE* outp; char buffer[100]; string outps = ""; outp = popen( message.body().c_str(), "r" ); while ( !feof(outp) ) { fgets( buffer, 100, outp ); outps = outps + buffer; } pclose(outp); cout &lt;&lt; "&amp; Output from command: " &lt;&lt; outps &lt;&lt; endl; m_session-&gt;send( outps.c_str(), "Output" ); </code></pre> <p>In <code>message.body();</code> is <code>string</code> which I want to run (I'm receiving this from <code>XMPP</code>). When the <code>string</code> is for example <code>"ls"</code>, it returns <code>string</code> with list of files in actual directory. But when the message is <code>"cd directory"</code>, nothing happens, like trying to change directory in scripts.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload