Problem with this code

Status
Not open for further replies.

nasha

Beta member
Messages
1
This code is not working properly because it dont open the requested file. I am not able to find out what the error is?? can anyone fix the error for me??

Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>


int main(int argc, char *argv[]) {
int fd;
int status;
char test[80] = "ls";
argv[0] = test;
	if ((fd = open("/home/test/test.txt", O_WRONLY)) == -1)
		printf("ERROR\n");

	close(1);
	dup2(fd, 1);
	close(fd);
	if (execvp(argv[0], argv) == -1)
		printf("EXEC ERROR\n");
	waitpid(-1, &status, 0);

return(0);
}
 
Status
Not open for further replies.
Back
Top Bottom