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

  • 本文关键字:访问 SFTP 代码 sftp
  • 更新时间 :
  • 英文 :


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

编辑:

while ((dirp = readdir (directory)) != NULL) { 
  if ( strstr(dirp->d_name , ".txt" )) { 
    printf( "found a .txtfile: %sn", 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] != '') {
                printf("%sn", 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("%sn", mem);
            }
        }
        else
            break;
    } while (1);

对于其他api信息,您可以通过SFTP api文档并尝试。http://www.libssh2.org//libssh2_sftp_readdir_ex.html

最新更新