Code Snippets

  

Visual Basic Source Code


Welcome to Dream.In.Code
Become a VB Expert!

Join 148,816 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,122 people online right now. Registration is fast and FREE... Join Now!





Reading a Text file Line by Line

Reads and puts value in a textbox in its original text format.

Submitted By: Xenon
Actions:
Rating:
Views: 59,005

Language: Visual Basic

Last Modified: January 4, 2006
Instructions: add a textbox to the form and a command button, the names are default VB6 chosen names

Snippet


  1. '(general declaration)
  2. Dim nFileNum As Integer, sText As String, sNextLine As String, lLineCount As Long
  3. '==================================
  4.  
  5. Private Sub Command1_Click()
  6.  
  7. ' Get a free file number
  8. nFileNum = FreeFile
  9. ' Open a text file for input. inputbox returns the path to read the file
  10. Open "C:\Documents and Settings\Sudeep\My Documents\3.txt" For Input As nFileNum
  11. lLineCount = 1
  12. ' Read the contents of the file
  13. Do While Not EOF(nFileNum)
  14.    Line Input #nFileNum, sNextLine
  15.    'do something with it
  16.    'add line numbers to it, in this case!
  17.    sNextLine = sNextLine & vbCrLf
  18.    sText = sText & sNextLine
  19. Loop
  20. Text1.Text = sText
  21. ' Close the file
  22. Close nFileNum
  23.  
  24. End Sub
  25.  

Copy & Paste


Comments


karlamohan 2008-08-22 01:17:09

The code is very usefull programmers like me Thank you

Rozie0910 2008-10-15 18:47:01

can i apply this coding in vbnet?


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month