c++sftplibssh

File returns NULL Value while using sftp_open() for copying file from local to remote using SFTP libssh in C++


I have Connected to the server using SFTP SSH in c++ and i have already copied files from remote to local. Now i'm trying to copy from local to remote, i read about this from this, but its not working. I wrote the following code.

 fr = fopen("C:/Users/Sami/Desktop/we/s.txt", "r");
fseek(fr, 0, SEEK_END);
 lSize = ftell(fr);
rewind(fr);
char * buffer = (char*)malloc(sizeof(char)*lSize);
 result = fread(buffer, 1, lSize, fr);
file = sftp_open(sftp, "/home/serversj/Desktop/sami/s.txt", O_CREAT, 1);
nwritten = sftp_write(file, buffer, result);

Where i opened the local system file using 'fopen' and store the file data into buffer the read the buffer content into result. While debugging i can see the sftp_open function creates a NULL value in 'file', hence the sftp_write shows errors 'No such file'. I'm working on windows. I got the error is in sftp_open , i also tried this answer but issues in the 'sftp_open'. I don't know how to correct this ..I'm stuck in this.

The error is : ‘SFTP server : Permission denied.’.. and ssh_get error is ‘No such file ‘


Solution

  • You were using sftp_open(); only for creating the file by using access_type O_CREAT,you need O_WRONLY writing some data into that file.

     fr = fopen("C:/Users/Sami/Desktop/we/s.txt", "r");
        fseek(fr, 0, SEEK_END);
         lSize = ftell(fr);
        rewind(fr);
        char * buffer = (char*)malloc(sizeof(char)*lSize);
         result = fread(buffer, 1, lSize, fr);
        int access_type = O_WRONLY | O_CREAT;
        file = sftp_open(sftp, "/home/serversj/Desktop/sami/s.txt", access_type,S_IRWXU);
        nwritten = sftp_write(file, buffer, result);
    

    I hope this will work..