0

Hello, I am trying to as the title says within linux environment within my C program this is what I have so far:

int file_out = open(filename_var,O_RDWR|O_CREAT);

if((child_pid = fork()) >= 0) /* successful fork */
{
	if(child_pid == 0) { /* Child process */
		close(1);
		dup(file_out);
		close(file_out);

		f(execvp(command_var,command_args) == -1) {
			printf("command not found\n");
		}

		_exit(0);
	}
	else { /* Parent Process */
		waitpid(-1,&status,0);
	}
}
else { /* fork() call unsuccessful */
	perror("fork");
}

It works how I want it to by the fact that it redirects the output to the filename given in filename_var but for some reason it also prints out some extra data, for example if I type 'echo helloworld > newfile.txt' within my program, it will write to the file correctly but then it'll also print out "helloworld > newfile.txt" to stdout. I am not sure what the problem may be, I tried using fflush() but it didn't work. I am not sure what the problem may be, thanks.

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by csurfer
0

Maybe, you're receiving in stdin the output generated by the execution of the proccess.

Try to close stdin also... only guessing.

0

In my view the usage 'echo helloworld > newfile.txt' within a 'C' program is limiting the redirection operator and as a result it is also being treated as as a simple '>' text character, which on echo prints the output 'helloworld > newfile.txt' to the stdout.

You need to look through this...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.