InputString Function

   
InputString Function

Class

Microsoft.VisualBasic.FileSystem

Syntax

 InputString(   filenumber, charcount   ) 
filenumber (required; Integer)

Any valid file number

charcount (required; Integer)

Number of characters to read from file

Return Value

A String containing charcount characters

Description

Reads data from a file into a string variable

Rules at a Glance

  • InputString should only be used with files opened in input ( OpenMode.Input ) or binary mode ( OpenMode.Binary ).

  • InputString begins reading from the current position of the file pointer.

  • InputString returns all the characters it reads, regardless of their type. This include spaces, carriage returns, linefeeds, commas, end-of-file markers, unprintable characters, etc.

  • Once the function finishes reading charcount characters, it also advances the file pointer charcount characters.

Example

If the file c:\data.txt contains the data:

 abcdefghijklmnopq 

the following code reads the characters, three at a time:

 Dim fr As Integer = FreeFile(  ) Dim sLine As String Dim i As Long FileOpen(fr, "c:\data2.txt", OpenMode.Input) For i = 1 To LOF(fr) \ 3    sLine = InputString(fr, 3)    Console.WriteLine(sLine) Next FileClose(fr) 

Programming Tips and Gotchas

  • InputString reads data written to a file using the Print , PrintLine , or FilePut functions.

  • InputString always attempts to precisely read charcount characters from the file. If there are no charcount characters from the position of the file pointer to the end of the file, InputString attempts to read beyond the end of the file, thereby generating an exception. To prevent this, you should use the LOF function after opening the file to ensure that you don't attempt to read past the end-of-file marker.

VB.NET/VB 6 Differences

  • Though a new function in VB.NET, InputString directly corresponds to the Input , Input$ , InputB , and InputB$ functions in VB 6.

  • The order of parameters is reversed in VB.NET and VB 6. In VB 6, the first parameter is charcount , and the second is filenumber .

  • The # symbol, which optionally preceded filenumber in VB 6, is not supported in VB.NET.

See Also

FilePut, FilePutObject Procedures, Print, PrintLine Procedures

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net