これは私の最初のCプログラムであり、私が確信が持てず、助けを求めて死にかけているいくつかのスポットがあります。これは、テキストファイル内のリンクからファイルをダウンロードするプログラムです。ありがとうございました!!!!!!
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
using std::printf;
FILE *file; /*declare the file pointer*/
char line [LINE_MAX];
//Parent process
int main()
{
pid_t pid;
file= fopen ("links.txt", "rt"); /*open file and read it*/
numberOfChildren = 0;
string url;
while (fgets (line,LINE_MAX, file) !=NULL) /*NOT SURE*/
++numberOfChildren;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/usr/bin/wget", "wget", <url>, NULL);/*NOT SURE*/
}
while (numberOfChildren>0) { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
--numberOfChildren;
printf ("Child Complete");
exit(0);
}
}