Cómo copiar un archivo en C / C ++ con libssh y sftp

Estoy publicando aquí porque necesito ayuda con algún código relacionado conlibssh.

Leí toda la documentación oficial.aquí pero aún no entiendo las cosas que necesito hacer si alguien me enciende, me sentiría complacido.

En realidad, quiero copiar un archivo de un cliente a un servidor remoto, pero no entiendo cómo hacerlo con la biblioteca libssh y la función sftp en libssh.

La situación es que: la sesión ssh está abierta y la sesión sftp también está abierta, puedo crear un archivo y escribir en él desde el cliente al servidor con la función integrada de lib ssh.

No encontré una manera fácil de copiar un archivo del cliente al servidor con una función simple comosftp_transfer (sourceFile (como c: \ my document \ hello world.txt), RemoteFile (/ home / user / hello world.txt), derecha (leer y escribir)) ?

Con lo que he entendido en el tutorial, primero está creando un archivo en la ubicación remota (servidor) y luego abre este archivo con esta línea de código:

file = sftp_open(sftp, "/home/helloworld.txt",access_type,1);

Después de eso, el archivo se crea en el servidor y luego se escribe en este archivo creado con un búfer:

const char *helloworld = "Hello, World!\n";
int length = strlen(helloworld);
nwritten = sftp_write(file, helloworld, length);

Mi pregunta ahora es si tengo un archivo, por ejemplo, un archivo .doc y quiero transferir / cargar ese archivo desde c: \ mydocument \ document.doc a un servidor remoto /home/user/document.doc remoto como puedo hacer ¿Con este método?

¿Cómo puedo poner este archivo en elsftp_write () ¿Función para enviarlo como el helloworld en la función de muestra?

Puede que no sea lo suficientemente bueno como para entender la programación, pero realmente traté de entenderlo y estoy atascado con eso.

Gracias de antemano por tu ayuda

Vea a continuación una muestra del código que solía probar:

// 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;
        }

` 

Respuestas a la pregunta(3)

Su respuesta a la pregunta