Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

popen

Status
Not open for further replies.

paddgu77

Programmer
Feb 28, 2005
5
US
Hi

I need to used popen to call a process and execute multiple commands.

e.g FILE *fd = popen("/bin/sh", "w");
while(condition) {
fwrite(each_time_command_changes,..,..,fd);
}
pclose(fd);

I have hard time to get output in between each command execution. The program sends the output only after executiong pclose()(prints all output together).

It would be gr8 if any one can help me with this issue.

thanks
pad.
 
As usually in buffered i/o, try fflush(fd) after fwrite(), may be it helps...
That's my test (it works on Windows with _-prefixed names):
Code:
/* Piper consumer:
int main()
{
	char buff[128];
	while (gets(buff))
		printf("%.80s\n",buff);
	return 0;
}
*/
int main() /* Producer: */
{
	FILE* f;
	f = _popen("piper.exe","w");
	if (f)
	{
		fputs("Pipe 1\n",f);
		fflush(f);
		system("pause");
		fputs("Pipe 2 end...\n",f);
		fflush(f);
		system("pause");
		_pclose(f);
	}
	return 0;
}
 
Depends on the programs you are executing through the pipe.
Sometimes interactive programs have internal buffering issues
that can't be solved without the use of ptys.
 
But the problem is popen with "/bin/sh". How could I fflush the internal buffer stored by this shell.

How to use ptrs with popen. Could you please give some hints about how to do that.

thanks for ur help
pad.
 
paddgu77 said:
How to use ptrs with popen. Could you please give some hints about how to do that.

ptys, not ptrs. pty = pseudo-terminal.

For the first hint, you'd probably want to look in your library documentation to see if it supports ptys, and to see what they are and how to use them. If you're using GLibC, the documentation is in the info file under "Low Level Terminal Interface," I believe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top