The following example illustrates the use of the
Read method.
JScript
function GetHeader()
{
var fso, f;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true);
f.Write("Header");
f.Write("1234567890987654321");
f.Close();
f = fso.OpenTextFile("c:\\testfile.txt", ForReading);
return(f.Read(6));
}
VBScript
Function ReadTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
f.Close
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ReadTextFileTest = f.Read(5)
End Function
PowerBASIC
FUNCTION GetHeader () AS STRING
LOCAL fso AS IFileSystem
LOCAL f AS ITextStream
LOCAL s AS STRING
fso = NEWCOM ("Scripting.FileSystemObject")
f = fso.OpenTextFile(UCODE$("c:\testfile.txt"), %IOMode_ForWriting, %VARIANT_TRUE)
f.Write UCODE$("Header")
f.Write(UCODE$("1234567890987654321"))
f.Close
f = fso.OpenTextFile(UCODE$("c:\testfile.txt"), %IOMode_ForReading)
s = f.Read(6)
f.Close
FUNCTION = ACODE$(s)
END FUNCTION
FUNCTION ReadTextFileTest () AS STRING
LOCAL fso AS IFileSystem
LOCAL f AS ITextStream
LOCAL s AS STRING
fso = NEWCOM ("Scripting.FileSystemObject")
f = fso.OpenTextFile(UCODE$("c:\testfile.txt"), %IOMode_ForWriting, %VARIANT_TRUE)
f.Write UCODE$("Hello world!")
f.Close
f = fso.OpenTextFile(UCODE$("c:\testfile.txt"), %IOMode_ForReading)
s = f.Read(5)
f.Close
FUNCTION = ACODE$(s)
END FUNCTION