一个令牌导致分段错误而另一个令牌不会

时间:2022-04-07 14:31:25

I have a function that reads filenames from directories. I want to extract these names, put them in an array for later use, fullname, namepart and extension part (3 different arrays). My function is able to write fullname and namepart NOT the extensions. Can anybody help me, here is my code extract; token 1 can be copied into the array, but copying token 2 results in segmentation fault;

我有一个从目录中读取文件名的函数。我想提取这些名称,将它们放在一个数组供以后使用,fullname,namepart和extension part(3个不同的数组)。我的函数能够写出fullname和namepart而不是扩展名。任何人都可以帮助我,这是我的代码提取;令牌1可以复制到数组中,但复制令牌2会导致分段错误;

while((entry=readdir(dirp2))!=NULL) 
            {
                strcpy(t_filename,entry->d_name);
                exception1=strcmp(entry->d_name,".");
                exception2=strcmp(entry->d_name,"..");
                exception3=strcmp(entry->d_name,".svn");
                if((exception1!=0)&&(exception2!=0)&&(exception3!=0))
                {
                    num_files++;
                    extension[num_files-1]=(char*)malloc(20*sizeof(char));
                    strcpy(t_string, entry->d_name);
                    token1=strtok(t_string,".");
                    token2=strtok(NULL,".");
                    strcpy(extension[num_files-1],token1);       
                }

            }

1 个解决方案

#1


if you, on a *inx platform, open a terminal and enter 'ls -al' you will see that the '.' and '..' directory lines have no text name, therefore, no extension.

如果您在* inx平台上打开终端并输入'ls -al',您将看到'。'和'..'目录行没有文本名称,因此,没有扩展名。

so the posted code is trying to skip those entries.

所以发布的代码试图跳过这些条目。

The posted code is also trying to skip any directory name that ends in 'svn'

发布的代码也试图跳过以“svn”结尾的任何目录名称

the token1 is everything before the first '.' (or whole string if no '.' in name.

token1是第一个'。'之前的所有内容。 (如果名称中没有'。',则为整个字符串。

the token2 is everything between the first '.' and the end of the string. (or NULL if no '.' in string.

token2是第一个'。'之间的所有内容。和字符串的结尾。 (如果字符串中没有'。',则为NULL。

the code is only keeping in the array, token1

代码只保留在数组token1中

Token1 can be NULL of the file name starts with a '.'

Token1可以为NULL,文件名以'。'开头。

to acquire a pointer to an 'extension' string.

获取指向“扩展”字符串的指针。

start at the end of the string and work back toward the front of the string, looking for a '.'

从字符串的末尾开始,然后回到字符串的前面,寻找一个'。'

if a '.' is not found or found in the first char position, then there is no extension so the pointer to the extension should be NULL

如果一个 '。'在第一个char位置找不到或找不到,那么没有扩展名,所以指向扩展名的指针应为NULL

#1


if you, on a *inx platform, open a terminal and enter 'ls -al' you will see that the '.' and '..' directory lines have no text name, therefore, no extension.

如果您在* inx平台上打开终端并输入'ls -al',您将看到'。'和'..'目录行没有文本名称,因此,没有扩展名。

so the posted code is trying to skip those entries.

所以发布的代码试图跳过这些条目。

The posted code is also trying to skip any directory name that ends in 'svn'

发布的代码也试图跳过以“svn”结尾的任何目录名称

the token1 is everything before the first '.' (or whole string if no '.' in name.

token1是第一个'。'之前的所有内容。 (如果名称中没有'。',则为整个字符串。

the token2 is everything between the first '.' and the end of the string. (or NULL if no '.' in string.

token2是第一个'。'之间的所有内容。和字符串的结尾。 (如果字符串中没有'。',则为NULL。

the code is only keeping in the array, token1

代码只保留在数组token1中

Token1 can be NULL of the file name starts with a '.'

Token1可以为NULL,文件名以'。'开头。

to acquire a pointer to an 'extension' string.

获取指向“扩展”字符串的指针。

start at the end of the string and work back toward the front of the string, looking for a '.'

从字符串的末尾开始,然后回到字符串的前面,寻找一个'。'

if a '.' is not found or found in the first char position, then there is no extension so the pointer to the extension should be NULL

如果一个 '。'在第一个char位置找不到或找不到,那么没有扩展名,所以指向扩展名的指针应为NULL