如何在 C 代码中使用 SFTP 访问特定目录

How to access the particular directory using SFTP in C code

我从未使用过 SFTP。请帮助我在c语言中使用SFTP访问目录。

编辑:

while ((dirp = readdir (directory)) != NULL) { 
  if ( strstr(dirp->d_name , ".txt" )) { 
    printf( "found a .txtfile: %s\n", dirp->d_name ); 
  } 
}

要使用SFTP访问目录,可以使用ssh库函数。

这是一些示例代码,

 do {
        char mem[512];
        char longentry[512];
        LIBSSH2_SFTP_ATTRIBUTES attrs;

        /* loop until we fail */ 
        rc = libssh2_sftp_readdir_ex(sftp_handle, mem, sizeof(mem),

                                     longentry, sizeof(longentry), &attrs);
        if(rc > 0) {

            if (longentry[0] != '[=10=]') {
                printf("%s\n", longentry);
            } else {
                if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
                  printf("--fix----- ");
                }
                else {
                    printf("---------- ");
                }

                if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
                    printf("%4ld %4ld ", attrs.uid, attrs.gid);
                }
                else {
                    printf("   -    - ");
                }

                if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
                    printf("%8" PRIu64 " ", attrs.filesize);
                }

                printf("%s\n", mem);
            }
        }
        else
            break;

    } while (1);

对于其他 api 信息,您可以查看 SFTP API 文档并尝试一下。 http://www.libssh2.org//libssh2_sftp_readdir_ex.html