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.

Recommended Answers

All 2 Replies

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

Try to close stdin also... only guessing.

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.