0

I want to copy a file from a server to client,i have already connected the server and i can add some content in a particular file in the server. I have gone through this tutorials and this ,hence i wrote some codes based on the tutorials. but its not working. I knew that there is some error while getting the destination path.while debugging i got this error Unhandled exception thrown: read access violation. file was nullptr... and i found 'The error is in fd (sftp_file fd;), i assigned the path (client) "C:/Users/Sami/Desktop/" where i want to copy the wi.exe from "/home/server/Desktop/sa/wi.exe" this path (server).' How can i correct this?

access_type = O_RDONLY;
        file = sftp_open(sftp, "/home/server/Desktop/sa/wi.exe",access_type,0);
        fd = sftp_open(sftp,"C:/Users/Sami/Desktop/", O_CREAT, 0);
       nbytes = sftp_read(file, buffer, sizeof(buffer));
        nwritten = sftp_write(fd, buffer, nbytes);
    sftp_close(file);

1 Answers1

0

You should only use sftp_open, sftp_read and sftp_write for files which are on the remote computer. For files on the local computer just use the normal file functions and classes (e.g. fopen or fstream).

E.g.

access_type = O_RDONLY;
file = sftp_open(sftp, "/home/server/Desktop/sa/wi.exe",access_type,0);
FILE* fd = fopen("C:/Users/Sami/Desktop/wi.exe", "w");
nbytes = sftp_read(file, buffer, sizeof(buffer));
nwritten = fwrite(buffer, sizeof(char), nbytes, fd);
sftp_close(file);
fclose(fd);

Also you missed the file name on your destination path, as πάντα ῥεῖ commented.

john
  • 85,011
  • 4
  • 57
  • 81
  • Error in '**FILE* fd = fopen(sftp,"C:/Users/Sami/Desktop/wi.exe", "r");**' the syntax is **FILE * fopen ( const char * filename, const char * mode );**. Is it? –  Sep 17 '18 at 05:31
  • **file = sftp_open(sftp, "/home/server/Desktop/sa/wi.exe",access_type,0);** by using this i can access (open ) the file (wi.exe) from the server. then i want to copy the **wi.exe** file to this **"C:/Users/Sami/Desktop/"** path. But using this **FILE* fd = fopen(sftp,"C:/Users/Sami/Desktop/wi.exe", "r");** line , reading the file **wi.exe** in the directory. Before writing the file ,this line shows errors. –  Sep 17 '18 at 06:27
  • @sami Sorry I made a couple of mistakes, it's fixed now. The line should be `FILE* fd = fopen("C:/Users/Sami/Desktop/wi.exe", "w");` – john Sep 17 '18 at 06:44
  • Its, working. But for copying from server to client(Opposite process of this), how can i do? –  Sep 17 '18 at 07:38
  • Just switch everything around, use `fopen`, `fread` for the local file and `sftp_open`, `sftp_write` for the remote file. Change `access_type` to `O_RDWR` (I think), change the second parameter to `fopen` to `"r"`. Really all you have to remember is, like I said, use sftp_ functions for the remote file, use fopen functions for the local file. – john Sep 17 '18 at 07:53
  • Actually access_type should probably be `O_WRONLY|O_CREAT` but as you can probably tell I need to look these things up to know what to do. You should try and do the same. – john Sep 17 '18 at 07:56
  • **FILE* fd = fopen("C:/Users/Sami/Desktop/wi.exe", "r")** and read using fread **result = fread(buffer, 1, lSize, fd);** now the syntaxt for sftp_write is**sftp_write ( sftp_file file,const void * buf,size_t count)** How can i do this sftp_write after fread? –  Sep 17 '18 at 10:32
  • `sftp_write(sftp, buffer, result);` I think – john Sep 17 '18 at 10:53
  • **file = sftp_open(sftp, "/home/rs.txt", O_WRONLY, 0); nwritten = sftp_write(file, buffer, result);** , i used this. but it didn't works. I changed O_WRONLY into O_CREAT, then it will create an empty file on the destination. –  Sep 17 '18 at 11:40
  • @sami Check the value of `result`, is it zero? Did you remember to close your file, `sftp_close(file);`? – john Sep 17 '18 at 13:37
  • The result have value, i can see this while debugging. After result, i have used sftp_open ,sftp_write, sftp_close(file); fclose(fr); . While writing ,its shows error **Permission denied ** –  Sep 18 '18 at 05:25
  • @sami Well if you don't have permissions to write to the server then there's nothing you can do with this code. Somehow you need to get the permissions before you run the program. – john Sep 18 '18 at 05:50
  • I have already connected with the server and i can edit the content in the file ,copy files from sever to client is already done by using [http://api.libssh.org/master/libssh_tutor_sftp.html] this, i think issues with **sftp_open(file,"",access_type,mode)** and issues relating the mode (Permission). –  Sep 18 '18 at 06:08
  • @sami Could be, I think you probably know more than me at this point. – john Sep 18 '18 at 06:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180248/discussion-between-sami-and-john). –  Sep 18 '18 at 06:30