I just need a little help... my program is pretty simple; it uses execlp to read a file, sort it, put the output into a different file, then close the original file. I've gotten this to work but it wouldn't end. I had to use Ctrl+C to stop the program and then the filename~ would have the sorted results but the actual file would be empty. However, in my playing around mixing up some options, I've not been able to get back to that point. Right now I am on a snag about file directories. Can someone point me where my code is wrong and maybe offer a little advice to help me advance?
Thanks, code is below:
Code:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
FILE *in_File;
int pid, fd;
/*open file with read permissions*/
if ((in_File = fopen(argv[1], "r+")) == NULL)
{
fprintf (stderr, "%s: cannot open %s for reading\n", argv[0], argv[1]);
exit(1);
}
/*create new process or quit */
if ( (pid = fork() ) == -1 ) {
perror("fork");
exit(1);
}
/*child does the work */
if (pid == 0) {
close(1);
fd = creat("userlist", 0644);
execlp("sort","sort",in_File,">", "userlist" , NULL);
perror("execlp");
exit(1);
}
/*parent waits then reports */
if(pid != 0) {
wait(NULL);
printf("Done running sort. Results in userlist\n");
fclose(in_File);
}
}