我从未使用过 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] != '\0') {
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
关于c - 如何在C代码中使用SFTP访问特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32599558/