2012-06-15 3 views
0

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~ 
+0

우연히'emacs'를 사용합니까? – FatalError

+0

@FatalError : 아니, 일식. – ron

답변

4

tilde (~)를 갖는가, 자동 백업입니다 인스턴스 Emacs tends to create these. 당신의 예제는 물결표가 붙은 텍스트 (.txt) 파일을 보여주기 때문에 이맥스가 여기에있는 것 같습니다.

코드에 아무런 문제가 없으므로 실제로 존재하는 도구이므로이 파일을 표시해야합니다.

+0

필자가 찾고자하는 파일이'given' 디렉토리 (검색을 시작할 라이브러리의 이름 인'argv [1]'과'argv [2]')에있는 경우, 내가 찾고자하는 파일의 이름 인 경우)'~'없이 나타나지만 하위 디렉토리 중 하나에 있으면 실제 파일이 나열되지 않고 '~'가 표시된 파일 만 나타납니다. 이상한 ? – ron

1

vi 또는 Vim을 편집기로 사용하고 있습니까? 해당 파일 (이름이 ~으로 끝나는 파일)은 vi으로 작성된 임시 파일입니다.

+0

아니요, Eclipse를 사용하고 있습니다. – ron

관련 문제