c - trying to redirect input and output using system calls -


i'm trying read input specific file , write specific file using unix(sys calls). output part must somehow created.

i want following: ./a.out < input.txt > output.txt using these functions: fork,dup.dup2,exec,open,write,read code :

pid_t   pid, runner; char *path = argv[1]; char enter[] = "/home/demo/desktop/os/ex1/ex12/code/input.txt"; char to[] = "/home/demo/desktop/os/ex1/ex12/ex1/ronen/"; char *out = "/home/demo/desktop/os/ex1/ex12/ex1/ronen/out1.txt"; char *args[] = {to, "./a.out","<",enter,">",out, null}; char *args2[] = {"cd", to, "./a.out","<",enter,">",to ,null}; if ((runner = fork()) < 0) {perror("could not make fork");} else if (runner == 0) {     printf("true\n");     execvp(args2[0],args2); } else if (runner != 0) {     waitpid(runner,0,0);     printf("done\n"); } 

so it's actaully working when put "a.out" file in desktop.

however has know how can redirect exec function go spefic folder? won't have pass executable file desktop?

for reading , writing files, use normal read , write functions; you'll need have file descriptor first, you'll need use fopen.

all in all, every c tutorial i've ever seen covers opening , writing long before touches fork, wonder you've been following. i'd recommend finding intro c , read first few pages; if understand fork does, piece of cake you.

edit: also, might misunderstand

program arguments <from_file >to_file  

does when using on shell:

the shell reads line, , calls program, passing arguments as... arguments. detects < , > on line , "bends" standard input , output file descriptors (yes, standard input , output behave that), fopened files.

program never sees <from_file nor >to_file.

edit in comment say:

i can not use fopen , functions. .fork,dup.dup2,exec,open,write,read

obviously, fopen wrapper open , returns file descriptor open returns.

also, program uses plenty libc functions; if you'd allowed write, shouldn't use printf, handy formatting wrapper puts, wrapper write. similar things apply all functions call, ie. waitpid, execvp, perror. should document requirements for yourself, first.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -