我正试图写一个c代码来扫描以.wor结尾的文件的整个目录,打开每个文件并从中提取某些信息。
到目前为止,我的代码成功打开了该目录下的所有文件,并扫描它们以寻找目标单词。我是否有办法修改代码,以便它只扫描以。word结尾的文件?我已经尝试了通配符"*"。但它只是给了我一个错误,说符号*和。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 boxn");
/* Open Directory*/
if (NULL == (FD = opendir ("/home/iselabs/dwang/pinscale/workorder/practice")))
{
fprintf(stderr, "Error : Failed to open input directory - %sn");
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,%sn", file, customer, device, testprog, software, dutboardid, corlbox);
fclose (fs) ;
printf("heyn");
c = c++; /*Increments c by 1 every time a file is read */
}
printf("total files scanned: %d n", c);
fclose ( ft ) ;
return 0;
}
您可以手动检查每个文件名是否以.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);