Xbase++
Xbase++
SFTP Read Text File
See more SFTP Examples
Demonstrates how to open a text file on the SSH server and read text.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oSftp
LOCAL cHostname
LOCAL nPort
LOCAL cHandle
LOCAL cSText
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oSftp := CreateObject("Chilkat.SFtp")
// Set some timeouts, in milliseconds:
oSftp:ConnectTimeoutMs := 5000
oSftp:IdleTimeoutMs := 15000
// Connect to the SSH server.
// The standard SSH port = 22
// The hostname may be a hostname or IP address.
cHostname := "sftp.example.com"
nPort := 22
nSuccess := oSftp:Connect(cHostname, nPort)
IF (nSuccess != 1)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// Authenticate with the SSH server. Chilkat SFTP supports
// both password-based authenication as well as public-key
// authentication. This example uses password authenication.
nSuccess := oSftp:AuthenticatePw("myLogin", "myPassword")
IF (nSuccess != 1)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// After authenticating, the SFTP subsystem must be initialized:
nSuccess := oSftp:InitializeSftp()
IF (nSuccess != 1)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// Open a file for reading.
cHandle := oSftp:OpenFile("myTest.txt", "readOnly", "openExisting")
IF (oSftp:LastMethodSuccess != 1)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// Assume the file we are reading contains the following text:
// abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ
// (in ANSI format -- i.e. one byte per char).
// Read 26 bytes:
cSText := oSftp:ReadFileText(cHandle, 26, "ansi")
IF (oSftp:LastMethodSuccess != 1)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// Should print "abcdefghijklmnopqrstuvwxyz";
? cSText
// Read the next 10 bytes.
cSText := oSftp:ReadFileText(cHandle, 10, "ansi")
IF (oSftp:LastMethodSuccess != 1)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// Should print "1234567890";
? cSText
// Read the next 26 bytes.
cSText := oSftp:ReadFileText(cHandle, 26, "ansi")
IF (oSftp:LastMethodSuccess != 1)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// Should print "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
? cSText
// Close the file.
nSuccess := oSftp:CloseHandle(cHandle)
IF (nSuccess != 1)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
? "Success."
oSftp:destroy()