简体   繁体   English

为什么在我的char *字符串中出现这种讨厌的波浪号〜?

[英]Why does this annoying tilde ~ come along in my char* string?

I'm trying to find a file using BFS and recursion , where given a directory and a file , the algorithm needs to check the given directory or all its sub directories . 我正在尝试使用BFS和递归查找文件,给定目录和文件,该算法需要检查给定目录或其所有子目录。

Now, when I check a directory, sometimes I have an annoying ~ tilde that pops up at the end of the char* string . 现在,当我检查目录时,有时在char* string的末尾会弹出一个烦人的~波浪号。 I've checked with some printf-S and came to a conclusion that the ~ is actually a part of the file-name . 我检查了一些printf-S并得出结论, ~实际上是文件名的一部分。

How could that be? 怎么可能 Sid I do something wrong? 难道我做错了什么?

Here is part of the recursion : 这是递归的一部分:

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);
}

And from terminal I hit ./exer4 check david.txt , and got : 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~

Having a tilde (~) as a suffix means the file is an automatic backup, for instance Emacs tends to create these . 波浪号(〜)作为后缀意味着该文件是自动备份,例如Emacs倾向于创建这些文件 Since your example shows tilde-suffixed text ( .txt ) files, it seems very likely that Emacs is the culprit here. 由于您的示例显示了带波浪号后缀的文本( .txt )文件,因此Emacs很有可能是此处的罪魁祸首。

So there's nothing wrong with your code, any tool should show these files since they're actually there. 因此,您的代码没有错,任何工具都应显示这些文件,因为它们实际上已经存在。

Are you using vi or Vim as an editor? 您是使用vi还是Vim作为编辑器? Those files (whose names end with a ~ ) are temporary files created by vi . 这些文件(其名称以~结尾)是vi创建的临时文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM