0

我正在尝试编写 ac 代码来扫描整个目录以查找以 .wor 结尾的文件,打开每个文件并从中提取某些信息。
到目前为止,我的代码已成功打开该目录中的所有文件,并扫描它们以查找目标词。有没有办法让我修改代码,使其只扫描以 .wor 结尾的文件?我尝试了通配符“*.wor”,但它只是给我一个错误,说符号 * 和 .wor 无法识别。
非常感谢你的帮助!
(顺便说一句,我最后添加的计数器也无法工作,我尝试使用整数“c”在每次读取文件时递增,并显示最后读取的文件总数。它只是给出我现在是 0。)

这是我的代码:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <errno.h>

    int main(int argc, char **argv) {

char file[100];
char buff[100];
char delims[] = " :=";
char *result = NULL;
char *customer;
char *device;
char *testprog;
char *software;
char *dutboardid;
char *corlbox;
int i=0;
DIR * FD;
struct dirent* in_file;
int c = 0;


    FILE * ft = fopen ( "WorkOrderInfo.csv", "w" ) ;    /* Open file to write to*/
    if ( ft == NULL )
    {
           puts ( "Cannot open target file" ) ;
           exit( 1 ) ;
    }

    fprintf (ft, "Work Order,Customer,Device,Test_Prog,Software,DUT_board_id,Corl box\n");



/* Open Directory*/

if (NULL == (FD = opendir ("/home/iselabs/dwang/pinscale/workorder/practice"))) 
    {
    fprintf(stderr, "Error : Failed to open input directory - %s\n");
    fclose(ft);

    return 1;
    }


    while ((in_file = readdir(FD))) 
    {

    if (!strcmp (in_file->d_name, "."))
        continue;
    if (!strcmp (in_file->d_name, ".."))    
        continue;


    /* Open files to read from  */


    FILE * fs = fopen(in_file->d_name, "r");
    if (fs == NULL)
    {
        puts ("Cannot open source file");

        return 1;
    }

    /* Scanning each file for targeted words: */


while( fgets(buff, 100,fs) != NULL )      
{   
 result = strtok( buff, delims );          
    while(result != NULL){                   
            if((strcmp(result,"Customer")==0)){ 
        result = strtok(NULL,delims);  
        customer = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(customer, result);
        for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
        customer[i] = ' ';
        }


            if((strcmp(result,"name")==0)){ 
        result = strtok(NULL,delims);  
        customer = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(customer, result);
        for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
        customer[i] = ' ';
        }

            if(strcmp(result,"device")==0){ 
        result = strtok(NULL,delims);  
        device = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(device, result);
        for(i=0;i<strlen(device)+1;i++){ if(device[i] == '\n') break; } 
        device[i] = ' ';
        }


            if(strcmp(result,"test_prog")==0){ 
        result = strtok(NULL,delims);  
        testprog = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(testprog, result);
        for(i=0;i<strlen(testprog)+1;i++){ if(testprog[i] == '\n') break; } 
        testprog[i] = ' ';
        }

            if(strcmp(result,"Rev")==0 || strcmp(result,"use")==0){ 
        result = strtok(NULL,delims);  
        software = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(software, result);
        for(i=0;i<strlen(software)+1;i++){ if(software[i] == '\n') break; } 
        software[i] = ' ';
        }

        if(strcmp(result,"rev")==0){ 
        result = strtok(NULL,delims);  
        software = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(software, result);
        for(i=0;i<strlen(software)+1;i++){ if(software[i] == '\n') break; } 
        software[i] = ' ';
        }

            if(strcmp(result,"DUT_board_id")==0){ 
        result = strtok(NULL,delims);  
        dutboardid = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(dutboardid, result);
        for(i=0;i<strlen(dutboardid)+1;i++){ if(dutboardid[i] == '\n') break; } 
        dutboardid[i] = ' ';
        }                   
        else if (strcmp(result,"DUT_board_id")==1){
        corlbox = "N/A";
        }

            if(strcmp(result,"box")==0){ 
        result = strtok(NULL,delims);  
        corlbox = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(corlbox, result);
        for(i=0;i<strlen(corlbox)+1;i++){ if(corlbox[i] == '\n') break; } 
        corlbox[i] = ' ';
        }
        else if (strcmp(result,"box")==1){
        corlbox = "N/A";
        }           
        result = strtok(NULL,delims);
    }

}


fprintf (ft, "%s,%s,%s,%s,%s,%s,%s\n", file, customer, device, testprog, software, dutboardid, corlbox);


fclose (fs) ;
printf("hey\n");
c = c++;                             /*Increments c by 1 every time a file is read */
}

printf("total files scanned: %d \n", c);
    fclose ( ft ) ;
return 0;

}

4

1 回答 1

2

您可以手动检查每个文件名是否以.wor如下方式结尾:

size_t len = strlen(in_file->d_name);
if (len >= 4 && memcmp(in_file->d_name + len - 4, ".wor", 4) == 0)
{
    // The filename ends in ".wor" (case-sensitive).  Note that this also
    // accepts the file whose entire name is ".wor".
}

或者,您可以改为使用该glob(3)函数来获取与特定通配符匹配的文件名列表:

// Error checking omitted for expository purposes
glob_t globbuf;
glob("/home/iselabs/dwang/pinscale/workorder/practice/*.wor", 0, NULL, &globbuf);

for (size_t i = 0; i < globbuf.gl_pathc; i++)
{
    char *filename = globbuf.gl_pathv[i];
    // Process filename...
}

globfree(&globbuf);
于 2013-01-10T00:17:36.917 回答