VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

Visual Basic Source Code


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

Join 300,308 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,079 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: 111,604

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?

hasoon 2009-02-04 21:54:20

thank u very much for posting this.makes my life way easier. ^.^

nimrod 2009-04-23 03:27:17

great

darfun 2009-05-01 02:23:21

Nice code Xenon, this simple and easy to understand and use code really force me to encourage you for the good work. im a bigner and this code is really good for newbies. now i,ll be searching for the code to write to text files. thanks man


Add comment


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





Live VB Help!

Be Social

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

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month