C
C
Read Text from a Remote SFTP File (Sequential)
See more SFTP Examples
Demonstrates the Chilkat SFtp.ReadFileText method, which reads up to a number of bytes from the current position of an open handle and decodes them to text. The arguments are the handle, the maximum number of bytes, and the charset. This example reads in chunks until end-of-file.
Background: Reading a file in bounded chunks keeps memory use predictable regardless of file size, which is why the loop reads a fixed amount and repeats until
Eof. A subtlety worth knowing: the byte limit is applied to the encoded bytes, so a chunk boundary could in principle fall in the middle of a multibyte character — accumulating the pieces and decoding the whole avoids any such issue in practice here since the text is reassembled in a StringBuilder.Chilkat C Downloads
#include <C_CkSFtp.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkSFtp sftp;
int port;
const char *password;
const char *handle;
HCkStringBuilder sbContent;
int chunkSize;
BOOL reading;
const char *chunk;
success = FALSE;
// Demonstrates the SFtp.ReadFileText method, which reads up to a number of bytes from the current
// position of an open handle and decodes them to text. The 1st argument is the handle, the 2nd
// is the maximum number of bytes, and the 3rd is the charset.
sftp = CkSFtp_Create();
// Connect, authenticate, and initialize the SFTP subsystem.
port = 22;
success = CkSFtp_Connect(sftp,"sftp.example.com",port);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
password = "mySshPassword";
success = CkSFtp_AuthenticatePw(sftp,"mySshLogin",password);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
success = CkSFtp_InitializeSftp(sftp);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
// Open the remote file for reading.
handle = CkSFtp_openFile(sftp,"subdir/notes.txt","readOnly","openExisting");
if (CkSFtp_getLastMethodSuccess(sftp) == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
// Read the file in chunks until end-of-file, accumulating the text.
sbContent = CkStringBuilder_Create();
chunkSize = 4096;
reading = TRUE;
while (reading) {
chunk = CkSFtp_readFileText(sftp,handle,chunkSize,"utf-8");
if (CkSFtp_LastReadFailed(sftp,handle)) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
CkStringBuilder_Dispose(sbContent);
return;
}
CkStringBuilder_Append(sbContent,chunk);
// Stop once the end of the file has been reached.
reading = !CkSFtp_Eof(sftp,handle);
}
success = CkSFtp_CloseHandle(sftp,handle);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
CkStringBuilder_Dispose(sbContent);
return;
}
printf("%s\n",CkStringBuilder_getAsString(sbContent));
CkSFtp_Disconnect(sftp);
CkSFtp_Dispose(sftp);
CkStringBuilder_Dispose(sbContent);
}