title: How to copy a file in C/C++ with libssh and SFTP
body: <p>I want to copy a file from a client to a remote server, but I don't understand how to do it with the libssh library SFTP API.</p>
  <p>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 libssh.</p>
  <p>I did not find an easy way to copy a file from the client to the server with a simple function like <strong>sftp_transfer(sourceFile(like c:\my document\hello world.txt),RemoteFile(/home/user/hello world.txt),right(read and write))</strong> ?</p>
  <p>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:</p>
  <pre><code>file = sftp_open(sftp, &quot;/home/helloworld.txt&quot;,access_type,1);
  </code></pre>
  <p>After that the file is created on the server, and then it is writing into this created file with a buffer:</p>
  <pre><code>const char *helloworld = &quot;Hello, World!\n&quot;;
  int length = strlen(helloworld);
  nwritten = sftp_write(file, helloworld, length);
  </code></pre>
  <p>My question is now if I have a file for example a .doc file and I want to transfer/upload that file from <code>c:\mydocument\document.doc</code> to  remote the remote server <code>/home/user/document.doc</code>, how can I do it with this method ?</p>
  <p>How can I put this file into the <code>sftp_write()</code> function to send it like the <a href="http://api.libssh.org/master/libssh_tutor_sftp.html" rel="nofollow noreferrer">helloworld in the sample function</a>?</p>
  <p>I may be not good enough in programming to understand, but I really tried to understand it and I'm stuck with it.</p>
  <p>Thanks in advance for your help</p>
  <p>See below a sample of the code I used to test:</p>
  <pre><code>// 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 = &quot;Hello, World!\n&quot;;
  int length = strlen(helloworld);
  
  //Open a SFTP session
  sftp = sftp_new(my_ssh_session);
  if (sftp == NULL)
  {
      fprintf(stderr, &quot;Error allocating SFTP session: %s\n&quot;,
      ssh_get_error(my_ssh_session));
      return SSH_ERROR;
  }
  // Initialize the SFTP session
  rc = sftp_init(sftp);
  if (rc != SSH_OK)
  {
      fprintf(stderr, &quot;Error initializing SFTP session: %s.\n&quot;,
      sftp_get_error(sftp));
      sftp_free(sftp);
      return rc;
  }
  
  //Open the file into the remote side
  file = sftp_open(sftp, &quot;/home/helloworld.txt&quot;,access_type,1);
  if (file == NULL)
  {
      fprintf(stderr, &quot;Can't open file for writing: %s\n&quot;,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, &quot;Can't write data to file: %s\n&quot;,
      ssh_get_error(my_ssh_session));
      sftp_close(file);
      return SSH_ERROR;
  }
  </code></pre>