Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > How do I read an entire file in...


Reply
 
Thread Tools Display Modes
  #1  
Old 11-03-2002, 05:13 PM
SolarCoasters SolarCoasters is offline
Centurion
 
Join Date: Oct 2002
Location: Florida
Posts: 164
Default How do I read an entire file in...


For my application i have my users able to type something into a text field then it gets saved to a text file but when I read in the file it only reads the first line how do I make VB read the entire file and store it in a variable.
Thankx...
Reply With Quote
  #2  
Old 11-03-2002, 05:57 PM
Termor's Avatar
Termor Termor is offline
Contributor

* Expert *
 
Join Date: Oct 2002
Location: Illinois, USA
Posts: 569
Default

You probably could have found this pretty quickly if you did a search, but here is the code to read the entire file into a variable.
Code:
Dim intFileNum As integer intFileNum = FreeFile() Open "C;\test.txt" For Input As #intFileNum YourVariable = Input$(LOF(intFileNum), intFileNum) Close #intFileNum

Edit: Added Open and close code.
Reply With Quote
  #3  
Old 11-03-2002, 07:38 PM
chrystal_ph
Guest
 
Posts: n/a
Lightbulb ReadAll

Hi!

If you are using FileSystemObject, you can try the ReadAll method. This returns the contents of a file in a string :


Function ReadFile() As String
Dim objFSO as New FileSystemObject
Dim objTextStream as TextStream
Set objTextStream = objFSO.OpenTextFile("yourfile.txt",ForReading,False)
ReadFile = objTextStream.ReadAll()
Set objTextStream = Nothing
Set objFSO = Nothing
End Function
Reply With Quote
  #4  
Old 11-04-2002, 03:48 AM
Hasin's Avatar
Hasin Hasin is offline
Centurion
 
Join Date: Sep 2002
Location: Bangladesh
Posts: 118
Default

Above codes are for TextFile or Text Stream. If you use a binary file then this code send you a message that interrupted by null terminating character. So please open all files as binary. You may do this with all type of file.

[vb]
dim j() as byte
open "filaname" for binary as #1
redim j(lof(1)) as byte
get #1,,j
close #1
[vb]

Now you have the entire file in "j" array. You may use and modify as you want.
__________________
Hasin Hayder
http://www.vbsthopoti.cjb.net
Lead Programmer - EvelinDev
Things that are tough takes time - Things that are impossible takes a little more.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->