FileSystem.InputString Method (Int32, Int32)

 

Returns String value that contains characters from a file opened in Input or Binary mode. The My feature gives you better productivity and performance in file I/O operations than InputString. For more information, see FileSystem.

Namespace:   Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static string InputString(
	int FileNumber,
	int CharCount
)

Parameters

FileNumber
Type: System.Int32

Required. Any valid file number.

CharCount
Type: System.Int32

Required. Any valid numeric expression specifying the number of characters to read.

Return Value

Type: System.String

Returns String value that contains characters from a file opened in Input or Binary mode. The My feature gives you better productivity and performance in file I/O operations than InputString.

Exception Condition
IOException

FileNumber does not exist.

ArgumentException

CharCount < 0 or > 214.

The InputString function is provided for backward compatibility and may affect performance. For non-legacy applications, the My.Computer.FileSystem object provides better performance. For more information, see File Access with Visual Basic.

Data read with the InputString function is usually written to a file by using Print or FilePut. Use this function only with files opened in Input or Binary mode.

Unlike the Input function, the InputString function returns all the characters it reads. This includes commas, carriage returns, line feeds, quotation marks, and leading spaces.

With files opened for Binary access, an attempt to read through the file by using the InputString function until EOF returns True generates an error. Use the LOF and Loc functions instead of EOF when you read binary files by using InputString, or use FileGet when you use the EOF function.

System_CAPS_noteNote

When reading from files, do not make security decisions about the contents of the file based on the file name extension. For example, a file named Form1.vb may not be a Visual Basic source file.

This example uses the InputString function to read one character at a time from a file and print it to the Output window. This example assumes that MyFile is a text file that has several lines of sample data.

Dim oneChar As String
' Open file.
FileOpen(1, "MYFILE.TXT", OpenMode.Input)
' Loop until end of file.
While Not EOF(1)
    ' Get one character.
    oneChar = (InputString(1, 1))
    ' Print to the output window.
    System.Console.Out.WriteLine(oneChar)
End While
FileClose(1)

.NET Framework
Available since 1.1
Return to top
Show: