我的代码中有以下内容:(用 c 编码)
ftw(argv[2], parseFile, 100)
argv[2] 是本地目录路径。例如。argv[2] = "TestCases" 并且在与我的 .o 文件相同的目录中有一个 testcases 文件夹。
我的理解是,这应该遍历目录 TestCases 并将找到的每个文件发送到函数 parseFile。
实际发生的是它只是将我的参数发送给函数 parseFile ,仅此而已。我究竟做错了什么?我应该如何正确使用它?
编辑:这是解析文件:
int parseFile(const char * ftw_filePath,const struct stat * ptr, int flags){
FILE * file;
TokenizerT * currFile;
char fileString[1000], * currWord, * fileName;
fileName = strdup(ftw_filePath);
if( fileName == NULL || strlen(fileName) <= 0){
free(fileName);
return -1;
}
printf("\n%s\n",fileName);
if(strcmp(fileName,"-h")== 0){
printf("To run this program(wordstats) type './wordstat.c' followed by a space followed by the file's directory location. (e.g. Desktop/CS211/Assignment1/test.txt )");
free(fileName);
return 1;
}
else{
file=fopen(fileName,"r");
}
if(!file){
fprintf(stderr,"Error: File Does not Exist in designated location. Please restart the program and try again.\n");
free(fileName);
return 0;
}
memset(fileString, '\0', 1000);
while(fscanf(file,"%s", fileString) != EOF){ /* traverses the file line by line*/
stringToLower(fileString);
currFile = TKCreate("alphanum",fileString);
while((currWord = TKGetNextToken(currFile)) != NULL) {
insert_List(currWord, words,fileName);
}
free(currFile->delimiters);
free(currFile->copied_string);
free(currFile);
memset(fileString, '\0', 1000);
}
fclose(file);
free(fileName);
return 1;
}
如果我为我的 argv[2] 输入 TestCases/big.txt 它将起作用,但如果我输入 TestCases 则不会