read input-process-output

Can you help me ?
I want to write a program ,which can open a input file (input.txt) and run as child process ,then write to output file (output.txt).......
[highlight=c]char inFile[1024]="input.txt";
char outFile[1024]="output.txt";
int main(int argc, char **argv)
{
pid_t pid=1;
int no=0; // no. of command
char command[1024]; // contain each line of input file
...........
while (pid && fgets(command,1023,iFile)!=NULL)
{
if (pid==0){

  //TODO: Child process
  //      Run command and write output to file
  //      Child process should sleep 1 second to wait parent prcess
  //        to finish writing to output file

else if \(pid>0\)

{

  //TODO: Parent process
  //      Write the no. of command to file
  //      Wait child process to finish

\}

[/highlight]

What help you want ?

  1. You have a good amount of notes on what to do, do it.

  2. Try out, if you have certain issues, come back and get advice on that.

  3. Place the code in proper code tags, for better visibility.

In child process :
system(command); //Run command

But I don't know how to write to output file ?

If it is appropriate, use like the following.,

system("/bin/ls >> /tmp/lsout");
  • system -- function call,
  • /bin/ls -- absolute path of command,
  • >> output redirection
  • /tmp/lsout -- absolute path of file to capture output.

you can simply use fork() and in child process use fwrite(command, output.txt). After fork, child and parent share open files.

Though It will be better if you post complete code and exactly what you wanted to do?

Can I use : system("%s >> %s",command,outFile) ?