这是我的第一个 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);
}
}