InputString Function
Returns String value containing characters from a file opened in Input or Binary mode.
The My feature gives you greater productivity and performance in file I/O operations than InputString. For more information, see My.Computer.FileSystem Object.
InputString(_
ByVal FileNumber As Integer, _
ByVal CharCount As Integer _
) As String
Parameters
FileNumber
Required. Any valid file number.CharCount
Required. Any valid numeric expression specifying the number of characters to read.
Exceptions
Exception type |
Error number |
Condition |
---|---|---|
FileNumber does not exist. |
||
CharCount < 0 or > 214. |
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.
Remarks
The InputString function is provided for backward compatibility and may have an impact on 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 with Print or FilePut. Use this function only with files opened in Input or Binary mode.
Unlike the Input function, the InputString function returns all of the characters it reads, including commas, carriage returns, line feeds, quotation marks, and leading spaces.
With files opened for Binary access, an attempt to read through the file using the InputString function until EOF returns True generates an error. Use the LOF and Loc functions instead of EOF when reading binary files with InputString, or use FileGet when using the EOF function.
Security Note: |
---|
When reading from files, do not make 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. |
Example
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 with a few 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)
Smart Device Developer Notes
This function is not supported.
Requirements
Namespace: Microsoft.VisualBasic
**Module:**FileSystem
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
See Also
Tasks
How to: Write Text to Files with a StreamWriter in Visual Basic
How to: Write Text to Files in Visual Basic