I post here because I need help with some libssh related code .
I read all the official documentation here , but still I don't understand what I need to do if someone can tell me that I would be happy.
Actually I want to copy the file from the client to the remote server, but I donβt understand how to do this with the libssh library and the sftp function in libssh.
The situation is this: the ssh session is open and the sftp session is also open, I can create a file and write it from the client to the server using the built-in lib ssh function.
I did not find an easy way to copy the file from the client to the server using a simple function such as sftp_transfer (sourceFile (e.g. c: \ my document \ hello world.txt), RemoteFile (/ home / user / hello world.txt), right (reading and writing)) ?
Given what I understood from the tutorial, a file is first created in a remote location (server), then it opens this file using 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 written to this created file with the buffer:
const char *helloworld = "Hello, World!\n"; int length = strlen(helloworld); nwritten = sftp_write(file, helloworld, length);
My question is now, if I have a file, for example, a .doc file, and I want to transfer / download this file from c: \ mydocument \ document.doc for the remote remote server /home/user/document.doc. can i do this with this method?
How can I put this file in the sftp_write () function to send it as helloworld to the sample function?
I may not be good at programming to understand, but I really tried to figure it out, and I was stuck with it.
Thank you in advance for your help.
The following is an example of the code I used for testing:
// 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 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; } `