ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i'm learning system programing for fun in unix. I'm trying to implment the redirection (>) arrow, like in unix the command ls -l > foo.txt writings the ouptut to the file names foo.txt
Code:
first fork to make child process...
wait of child process to end
in child:
go through the arguments untill get to >(arrow)
/* get the filename */
freopen(args[i+1], "w", stout);
/* the command line args */
execvp(*args, args);
fclose(stout);
when i run it, it just hangs. I read the man pages, and they say check the return values for errors. But the only error it gives me is 10, and i looked for what this number means, but no luck. If some could point me in the right direction and explain on how to go about redirecting a command to file.
thanks,
djgerbavor3
Last edited by djgerbavore; 01-14-2005 at 10:53 AM.
While it is probably a typo in your post, and not in your actual code, this line:
Code:
freopen(args[i+1], "w", stout);
should read:
Code:
freopen(args[i+1], "w", stdout);
To get a more understandable error-message, you can call the "perror()" function. It will print a error message about the last error that occurred in a library function.
An example for redirecting stdout of the program itself to a file given on its command line including checking for error,a nd printing a informative message with perror():
Code:
#include <stdio.h>
/*
* This program redirects stdout to a file given as the first command line
* argument, and then prints a message to stdout so it will appear in the file
* given on the command line.
*/
int main(int argc, char **argv)
{
if (argc != 2) { /* Check command line syntax. */
fprintf(stderr, "Usage: %s <file name>\n", argv[0]);
return 1; /* exit program with error exit code */
}
/* Redirect my own stdout to file given and check for error. */
if (freopen(argv[1], "w", stdout) == NULL) {
perror("Redirecting stdout"); /* Will give information about the error */
return 1; /* exit program with error exit code */
}
/* Printing text to stdout, which will end up in the file */
printf("Printing this to stdout, but it should appear in %s.\n", argv[1]);
return 0;
}
thank you, i'm making a little further. however freopen command isn't the one thats hanging up program, the command execvp is the one causing my program to do nothing
The first parameter of execvp() is a constant string, indicating the file to execute. In your code it should be args[0].
execv() and execvp() need to have NULL-terminated array. Are you sure that your args will have NULL at the end? Otherwise it will keep on looking for more arguments endlessly.
Code:
const char* args[4];
args[0]="ls";
args[1]="-l";
/* Without this execvp() will go over args[3], args[4], args[5].... args[9999].... until it sees NULL */
args[2]=NULL;
execvp(args[0], args);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.