如何使用libssh和sftp在C/C ++中复制文件 [英] How to copy a file in C/C++ with libssh and sftp
问题描述
我在这里发布是因为我需要一些与 libssh 相关的代码的帮助.
I'm posting here because I need help with some code relating with libssh.
我在此处阅读了所有正式文档,但我还是不明白如果有人可以照亮我,我需要做的,我会很高兴.
I read all the official documentation here but still I don't understand the things i need to do if someone can en light me I would be pleased.
实际上,我想将文件从客户端复制到远程服务器,但是我不知道如何使用libssh库和libssh中的sftp函数来完成此操作.
Actually I want to copy a file from a client to a remote server but I don't understand how to do it with the library libssh and the function sftp in libssh.
情况是:ssh会话已打开,sftp会话也已打开,我可以创建一个文件,并使用lib ssh的集成功能将其从客户端写入服务器.
The situation is that: The ssh session is open and the sftp session is open too, I can create a file and write in it from the client to the server with the integrated function of lib ssh.
我没有找到一种简单的方法通过简单的功能将文件从客户端复制到服务器,例如 sftp_transfer(sourceFile(例如c:\ my document \ hello world.txt),RemoteFile(/home/用户/您好,world.txt),对(读写))?
I did not find an easy way to copy a file from the client to the server with a simple function like sftp_transfer(sourceFile(like c:\my document\hello world.txt),RemoteFile(/home/user/hello world.txt),right(read and write)) ?
根据我对本教程的了解,它首先是在远程位置(服务器)创建一个文件,然后使用以下代码行打开该文件:
With what I have understood from the tutorial it is first creating a file in the remote location (server) then it is opening this file with this line of code :
file = sftp_open(sftp, "/home/helloworld.txt",access_type,1);
之后,在服务器上创建文件,然后使用缓冲区将其写入此创建的文件中:
After that the file is created on the server , and then it is writing into this created file with a buffer:
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,请执行以下操作:我可以用这种方法吗?
My question is now if I have a file for example a .doc file and I want to transfer/upload that file from c:\mydocument\document.doc to remote the remote server /home/user/document.doc how can I do it with this method ?
如何将该文件放入 sftp_write()函数中,使其像示例函数中的helloworld一样发送?
How can I put this file into the sftp_write() function to send it like the helloworld in the sample function ?
我可能不擅长编程,但我确实试图理解它,但我坚持了下去.
I may be not good enough in programming to understand, but I really tried to understand it and i'm stuck with it.
预先感谢您的帮助
请参见下面我用来测试的代码示例:
See below a sample of the code I used to test:
// 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;
}
`
推荐答案
以常规方式打开文件(使用C ++的 fstream 或C的 stdio.h ) ,将其内容读取到缓冲区中,然后将缓冲区传递给sftp_write
.
Open the file in the usual way (using C++'s fstream or C's stdio.h), read its contents to a buffer, and pass the buffer to sftp_write
.
类似这样的东西:
ifstream fin("file.doc", ios::binary);
if (fin) {
fin.seekg(0, ios::end);
ios::pos_type bufsize = fin.tellg(); // get file size in bytes
fin.seekg(0); // rewind to beginning of file
std::vector<char> buf(bufsize); // allocate buffer
fin.read(buf.data(), bufsize); // read file contents into buffer
sftp_write(file, buf.data(), bufsize); // write buffer to remote file
}
请注意,这是一个非常简单的实现.您可能应该以追加模式打开远程文件,然后将数据分块写入,而不是发送单个大块数据.
Note that this is a very simple implementation. You should probably open the remote file in append mode, then write the data in chunks instead of sending single huge blob of data.
这篇关于如何使用libssh和sftp在C/C ++中复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!