2014-03-02 191 views
0

我编辑了我以前的问题。使用wget命令执行

由于我得到了问题和改变了代码,现在我有一个不同的问题。如果我使用execle命令,它只会使用wget命令下载一个图像,否则如果wget命令没有执行,它会在屏幕上打印所有图像名称。我不明白什么时候有一个while循环,那么为什么它只打印一个图像。

#include<stdlib.h> 
#include<unistd.h> 
#include<string.h> 
#include<limits.h> 
#include<fcntl.h> 
#include<sys/stat.h> 
#include<sys/types.h> 
#include<stdio.h> 

void main(int argc, char*argv[]) 
{ 
int iFlag; 
char cline[100]; 
FILE*fil = fopen("index.html","rt"); 
if(fil==NULL) 
{ 
    printf("Error in opening file"); 
} 
char*tmpLine; 
char*tok; 
const char check[10] = "<img"; 
const char check2[10] = "src="; 
char images[50]; 
strcpy(images,argv[1]); 
while(fgets(cline,100,fil)!=NULL) 
{ 
    if(strstr(cline,check)!=NULL) 
    { 
     tmpLine=strstr(cline,check); 
     if(strstr(cline,check2)!=NULL) 
     { 
      tmpLine=strstr(cline,check2); 
      tok = strtok(tmpLine,"\""); 
      while(tok!=NULL) 
      { 
       tok = strtok(NULL,"\""); 
       if(tok[0]!='/') 
       { 
        strcat(images,"/"); 
        strcat(images,tok); 
        printf("\nimage: %s\n",images); 
        iFlag = execle("/usr/bin/wget","wget","-o","logfile",images,NULL); 
        if(iFlag<0) 
         perror("EXECLE ERROR"); 
        break; 
       } 
       else 
        break; 
      } 
      memset(&images[0], 50, sizeof(images)); 
      strcpy(images,argv[1]); 
     } 
    } 

} 

}

回答

2

的一个大问题是,exec家庭的功能替换你与这个新程序的过程。这意味着如果execle的调用成功,您的程序将不再存在。

如果你想要自己的程序继续,你需要fork一个新的过程。

+0

这是一个很好的替代品吗? char * tmp =“wget -o logfile”; char * temp =(char *)malloc(tmp +(sizeof(images))); strcpy(temp,tmp); strcat(temp,images); system(temp); – talbinzal