I wrote the following code from this.
#define MAX_XFER_BUF_SIZE 16384
int main()
{
char buffer[MAX_XFER_BUF_SIZE];
//..some content
access_type = O_RDONLY;
sftp_file file = sftp_open(sftp, "/home/ra/Desktop/sami/s.txt", access_type, 0);
ofstream fin("C:/Users/s.txt", ios::binary | ios::in | std::ofstream::out | std::ofstream::app);
nbytes = sftp_read(file, buffer, sizeof(buffer));
fin.write(buffer, sizeof(buffer));
But the buffer maximum size is predefined. So it only works for small files. I want to get the SFTP file size which size is much bigger. For that I need to send the data as chunks by append mode.
for (int i=0;i<sizeof(buffer);i++)
{
fin.write(buffer, buffer[i]);
}
I went through this answer, but i'm stuck in this > How can i solve this issue.
You have removed the loop that is in the Reading a file from the remote computer example.
Return it back. The important part of the example modified to use ofstream
is like:
char buffer[MAX_XFER_BUF_SIZE];
// ...
for (;;)
{
nbytes = sftp_read(file, buffer, sizeof(buffer));
if (nbytes == 0)
{
break; // EOF
}
else if (nbytes < 0)
{
fprintf(stderr, "Error while reading file: %s\n", ssh_get_error(session));
sftp_close(file);
return SSH_ERROR;
}
fin.write(buffer, nbytes);
if (!fin)
{
fprintf(stderr, "Error writing");
sftp_close(file);
return SSH_ERROR;
}
}
fin.close();
if (!fin)
{
fprintf(stderr, "Error writing");
sftp_close(file);
return SSH_ERROR;
}
Imo, fin
seems like a bad name to me. Shouldn't it be fout
("file output")?