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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I use execv to call sort and get the answer

Status
Not open for further replies.

koeicjm

Programmer
Sep 12, 2001
73
JP
how can I use execv to call sort and get the answer of sort ? can someone give me a sample.
 
Actually, the best way to do that would be to use popen(), this executes a program and pipes the output back to you. It returns a FILE * from which you then can read the output of the program that you have run.

You see, exec and its siblings will completely replace the program that calls it with the program executed by exec...thus a call to exec will _never_ return.

What you normally do is to use fork() to create a copy of the process that is running your program, in this copy you then run exec on the program that is to be run by your program. This gives you a new process running the program that you wanted to run from your own.

If you want the output of that program you also would have to create a pipe that you connect to standard output for the newly created process and the other end goes back to your program. This way your program can read what the new process outputs. All this is done bt popen to save you the headache =)
 
Here's an example:

If you have a file, sortfile.txt, containing the following 4 lines:
hello
world
popen
rocks

Then the following code will output the sorted contents:
#include <stdio.h>

int main()
{
char buffer[21];
FILE *file = popen(&quot;sort sortfile.txt&quot;, &quot;r&quot;);
int num_read = 0;
do {
num_read = fread((void*)buffer, 1, 20, file);
buffer[num_read] = '\0';
printf(&quot;%s&quot;, buffer);
} while (num_read >= 20);
return 0;
}

The output will be:
hello
popen
rocks
world

For more info on the standard C library (at least GNU's version of it) see
 
thank you , can I do it not from the sortfile.txt , I want to sort sth from the main(), I mean the input data is in my main(), such as char * [] = { &quot;hello&quot;,&quot;world&quot;,&quot;popen&quot;,&quot;rocks&quot;}, because I have an application just like sort but it can only read data from stdin now , I donot want to read from the file because I have a so large data to be &quot;sort&quot; . Why I ask this question is I want to know how can I use the application from my process.
 
Umm, well, If you have the data to be sorted in an array, wouldn't it be very much easier to use the qsort(3)-function?

In this case the code example would be:

#include <stdlib.h>
#include <stdio.h>

int my_cmp(const void *item1, const void *item2) {
return strcmp(*(const char **)item1, *(const char **)item2);
}

int main()
{
char *strings[] = {&quot;hello&quot;, &quot;world&quot;, &quot;qsort&quot;, &quot;rocks&quot;};
int i;

qsort((void *)strings, 4, sizeof(char *), my_cmp);
for (i = 0; i < 4; i++) {
printf(&quot;%s\n&quot;, strings);
}

return 0;
}

Or if you stored it in a vector<string> instead you could use the much easier code:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
vector<string> strings;
strings.push_back(&quot;hello&quot;);
strings.push_back(&quot;world&quot;);
strings.push_back(&quot;STL&quot;);
strings.push_back(&quot;rocks&quot;);
sort(strings.begin(), strings.end());
for (unsigned i = 0; i < strings.size(); i++) {
cerr << strings << endl;
}
}
 
thank you very much , I think I cannot express how I want , I have an binary file (have not source ), it can read input from stdin and output to stdout, I want use it from my main(), so I must use exec . What I want is calling it from my main(), and my main() must get the output too.
 
Oh, I see.

Well, that's trickier, as far as I know popen only supports that you connect the file to either stdin or stdout, not both. So I guess you'd have to fork, pipe and exec yourself.

Here's a link explaning how to use pipes in C:

And here's one on fork() and exec():

I don't know if I'll have time, but perhaps I'll try to make an example that does pretty much what you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top