Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将strstr()与文件名一起使用_C_Shell - Fatal编程技术网

C 如何将strstr()与文件名一起使用

C 如何将strstr()与文件名一起使用,c,shell,C,Shell,我正试图在一个小地狱中实现*shell通配符。 我编写了一个程序,将*之后的部分与当前目录的文件名进行比较 在做了一些测试之后,似乎不起作用的部分是strstrstr函数,它甚至在到达包含输入子字符串的文件名时也会发送空指针: if (strstr(ent->d_name,wildcard)!= NULL) 例如,如果我在包含名为filename的文件的文件夹中执行cat*filename,程序将读取“filename”,然后将其与“filename”进行比较,但会产生空指针 这就是代码

我正试图在一个小地狱中实现*shell通配符。 我编写了一个程序,将*之后的部分与当前目录的文件名进行比较

在做了一些测试之后,似乎不起作用的部分是strstrstr函数,它甚至在到达包含输入子字符串的文件名时也会发送空指针:

if (strstr(ent->d_name,wildcard)!= NULL)
例如,如果我在包含名为filename的文件的文件夹中执行cat*filename,程序将读取“filename”,然后将其与“filename”进行比较,但会产生空指针

这就是代码的样子

int wildcardCheck( char* input ) {
    int returnflag;

    if( strstr( input, "*" ) != NULL ) {
        int i;
        char* wildcard;
        wildcard = strstr( input, "*" );
        strcpy( wildcard, wildcard + 1 ); // +1 excludes wildcard (*)
        printf( "it's %s", wildcard );

        // Open current directory and get all entities
        struct dirent* ent;
        DIR* dir = opendir( "." );
        char* arg;

        while( (ent = readdir( dir )) != NULL ) {
            // If the entity filename matches the regular expression
            printf( " file %s\n", ent->d_name );
            if( strstr( ent->d_name, wildcard ) != NULL ) {
                arg = malloc( (sizeof( char ) * strlen( ent->d_name )) + 1 );
                strcpy( arg, ent->d_name );
                // Substitutes the argument containing the wildcard with the new filename
                input = strtok( input, "*" );

                // We only substitute one filename in this loop and add the rest later, hence break
                input = strcat( input, arg );
                printf( "arg is%s\n", arg );
                printf( "final input is%s", input );
                free( arg );
                returnflag = 1;
                break;
            }
            else printf( "didn't enter %d : strstr( %s, %s ) == NULL\n", 3, ent->d_name, wildcard );
        }


        closedir( dir );

    }
以下是对当前程序的测试:

mysh-master$ cat *ilename
it's ilename


file filename
didn't enter 3 : strstr( filename, ilename
) == NULL

0
cat: ilename: No such file or directory
Exit status : 1
调试输出:

didn't enter 3 : strstr( filename, ilename ) == NULL 表示通配符==“ilename\n”-即它有一个换行符,因此在没有换行符的情况下将不匹配
“filename”

调试输出:

didn't enter 3 : strstr( filename, ilename ) == NULL
指示
通配符==“ilename\n”
-即,它有一个换行符,因此在没有换行符的情况下将不匹配
“filename”

strcpy(通配符,通配符+1)
可以使用重叠对象与
strcpy()
,尝试
memmove(通配符,通配符+1,strlen(通配符))
pmg所说的,或者更简单地说
wildcard++
-不需要复制或移动任何东西。噢!当然
通配符+++
!但为了完整起见,我保留了我的“超额完成者”注释。您的错误报告将更有效地报告
else printf(“未输入%d:strstrstr(%s,%s)==NULL\n”,3,ent->d_name,通配符)或只是使用调试器-比这更快!至少包括您的调试输出。@Clifford就是这么做的,我显然有一个文件名与我的输入
strcpy(通配符,通配符+1)匹配
可以使用重叠对象与
strcpy()
,尝试
memmove(通配符,通配符+1,strlen(通配符))
pmg所说的,或者更简单地说
wildcard++
-不需要复制或移动任何东西。噢!当然
通配符+++
!但为了完整起见,我保留了我的“超额完成者”注释。您的错误报告将更有效地报告
else printf(“未输入%d:strstrstr(%s,%s)==NULL\n”,3,ent->d_name,通配符)或只是使用调试器-比这更快!至少包括您的调试输出。@Clifford就是这么做的,我显然有一个与输入匹配的文件名