Я хочу скопировать файл от клиента на удаленный сервер, но я не понимаю, как сделать это с помощью API SFTP Libssh Library. Клиент на сервере с простой функцией, такой как sftp_transfer (sourcefile (например, c: \ my document \ hello world.txt), remotefile (/home/user/hello world.txt), справа (чтение и написание)) ? Код: < /p>
file = sftp_open(sftp, "/home/helloworld.txt",access_type,1);
< /code>
После этого файл создается на сервере, а затем он записывает в этот созданный файл с буфером: < /p>
const char *helloworld = "Hello, World!\n";
int length = strlen(helloworld);
nwritten = sftp_write(file, helloworld, length);
Мой вопрос теперь заключается в том, если у меня есть файл, например, файл .doc, и я хочу перенести/загрузить этот файл из C: \ mydocument \ document.doc , чтобы удалить удаленный сервер/home/user/document.doc , как я могу сделать это с этим методом? sftp_write () Функция отправки его как Helloworld в образце функции?// Set variable for the communication
char buffer[256];
unsigned int nbytes;
//create a file to send by SFTP
int access_type = O_WRONLY | O_CREAT | O_TRUNC;
const char *helloworld = "Hello, World!\n";
int length = strlen(helloworld);
//Open a SFTP session
sftp = sftp_new(my_ssh_session);
if (sftp == NULL)
{
fprintf(stderr, "Error allocating SFTP session: %s\n",
ssh_get_error(my_ssh_session));
return SSH_ERROR;
}
// Initialize the SFTP session
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
fprintf(stderr, "Error initializing SFTP session: %s.\n",
sftp_get_error(sftp));
sftp_free(sftp);
return rc;
}
//Open the file into the remote side
file = sftp_open(sftp, "/home/helloworld.txt",access_type,1);
if (file == NULL)
{
fprintf(stderr, "Can't open file for writing: %s\n",ssh_get_error(my_ssh_session));
return SSH_ERROR;
}
//Write the file created with what's into the buffer
nwritten = sftp_write(file, helloworld, length);
if (nwritten != length)
{
fprintf(stderr, "Can't write data to file: %s\n",
ssh_get_error(my_ssh_session));
sftp_close(file);
return SSH_ERROR;
}
Подробнее здесь: https://stackoverflow.com/questions/136 ... h-and-sftp
Как скопировать файл в C/C ++ с помощью Libssh и SFTP ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Libssh ssh_pki_import_pubkey_file возвращает ошибку, но ошибка не установлена
Anonymous » » в форуме C++ - 0 Ответы
- 14 Просмотры
-
Последнее сообщение Anonymous
-