BFS
과 재귀를 사용하여 파일을 찾으려고합니다. 주어진 디렉토리와 파일에서 알고리즘은 주어진 디렉토리 또는 모든 하위 디렉토리를 검사해야합니다.왜이 성가신 물결표가 내 char * 문자열에 표시됩니까?
이제 디렉터리를 검사 할 때 가끔 char*
문자열 끝 부분에 성가신물결표가 나타납니다. 나는 printf-S
으로 확인한 결과 ~
이 실제로 파일 이름의 일부라는 결론에 도달했습니다.
어떻게 될 수 있습니까? 시드 내가 뭐 잘못한거야? 여기
는 재귀의 일부입니다
int scanner(char *dirname,char *entries , char * directory , char * file)
{
struct stat st;
/* scan the directory and store the entries in a buffer */
DIR *dir;
struct dirent *ent;
int count=1;
char name[256];
if ((dir = opendir(dirname)) == NULL)
{
perror("Unable to open directory");
return(0);
}
while ((ent = readdir(dir)) != NULL)
count++;
rewinddir(dir);
// here we copy all the file-names from the directory into the buffer
// we copy all the names using sprintf and strcpy
while ((ent = readdir(dir)) != NULL)
{
strcpy(name,ent->d_name);
if (strcmp(name , file) == 0)
{
printf("\nFile was found !!! in first IF\n");
printf("\n-----------------------------------------------------------------------\n");
if (stat(name, &st) < 0) {
perror(name);
putchar('\n');
continue;
}
printfile(&st , name);
printf("\nStringer 'name' is : %s" , name);
printf("\nThe length of %s is %d" , name , strlen(name));
}
else // try
{
int length = strlen(name);
char try[length+2];
strcpy(try,name);
try[length+1] = '~';
try[length+2] = '\0';
printf("\nThe 'name' is : %s" , name);
printf("\nThe length of %s is %d" , name , strlen(name));
printf("\nPrint try %s\n" , try);
if (strcmp(try , file) == 0)
printf("\nFile was found !!! in second IF\n");
}
sprintf(entries,"%s",name);
entries = entries+strlen(name)+1;
printf("\nStringer name :%s" , name);
count++;
}
if (closedir(dir) != 0)
perror("Unable to close directory");
return(count);
}
그리고 terminal
에서 나는 ./exer4 check david.txt
를 공격하고 있어요 : 접미사가 파일을 의미로
The 'name' is : ..
The length of .. is 2
Print try ..
Stringer name :..
The 'name' is : .
The length of . is 1
Print try .
Stringer name :.
The 'name' is : insideCheck
The length of insideCheck is 11
Print try insideCheck
Stringer name :insideCheck
The 'name' is : david.txt~
The length of david.txt~ is 10
Print try david.txt~
Stringer name :david.txt~
The 'name' is : doc.txt~
The length of doc.txt~ is 8
Print try doc.txt~
우연히'emacs'를 사용합니까? – FatalError
@FatalError : 아니, 일식. – ron