Hagamos que México crezca..

Prefiere el consumo de lo Hecho en México

Prefiere el consumo de lo Hecho en México
BúsquedaHagamos que México crezca..
![]() Prefiere el consumo de lo Hecho en México Posts Recientes
Conversación |
How to run piped applications using CI have had many times problems to execute external applications from another application, for example I want run this command " ls | grep something " from any application, I can execute external applications using C by mean << system >> or better using << CreateProcess >> function, but especially with this format I have problems, because CreateProcess run the first command("ls -a") but dont pass the information generated by "ls command" to grep command, maybe < CreateProcess > function finish the child process after run "ls -a" but dont continue with "grep" command, the < CreateProcess >, < system > or < ShellExecute > functions dont work for this case. Then remembering that C can use pipes by mean programming, I search in google what is the way that I can do it.
For my good luck only I needed five minutes to resolve my legendary problem.. and the code is the following.
/* * From * [url]http://www.crasseux.com/books/ctutorial/Programming-with-pipes.html[/url] * but changed to use fgets() instead of the GNU extension getdelim() */ #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *ps_pipe; FILE *grep_pipe; int bytes_read; char buffer[100]; /* could be anything you want */ /* Open our two pipes ls -a | grep *~ */ ps_pipe = popen(" ls -a", "r"); grep_pipe = popen("grep *~", "w"); /* Check that pipes are non-null, therefore open */ if ((!ps_pipe) || (!grep_pipe)) { fprintf(stderr, "One or both pipes failed.\n"); return EXIT_FAILURE; } bytes_read = 0; while (fgets(buffer, sizeof(buffer), ps_pipe)) { fprintf(grep_pipe, "%s", buffer); bytes_read += strlen(buffer); } printf("Total bytes read = %d\n", bytes_read); /* Close ps_pipe, checking for errors */ if (pclose(ps_pipe) != 0) { fprintf(stderr, "Could not run 'ls', or other error.\n"); } /* Close grep_pipe, cehcking for errors */ if (pclose(grep_pipe) != 0) { fprintf(stderr, "Could not run 'grep', or other error.\n"); } getch(); /* Exit! */ return 0; } P.D. Excuse for my bad english.. I know that it Suck, but from now I need to practice my english for personal and professional reasons..
Fuentes XML de comentario: RSS | Atom
Estadísticas de visitantes186579 |
Nube de EtiquetasEventosEncuestaComentarios Recientes
|
Dejar un comentario