Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Carven:
To read from a file in VB6, try this:
Private Sub Reading_Click()
Dim variable1 As String
Open "c:\My Documents\sample.txt" For Input As #1
Input #1, variable1
Text1.Text = variable1
Close #1
End Sub--
To write to a file, try this:
Private Sub create_Click()
Dim intMsg As String
Dim StudentName As StringOpen "c:\My Documents\sample.txt" For Output As #1
intMsg = MsgBox("File sample.txt opened")
StudentName = InputBox("Enter the student Name")
Print #1, StudentName
intMsg = MsgBox("Writing a" & StudentName & " to sample.txt ")Close #1
intMsg = MsgBox("File sample.txt closed")
End Sub
--
Hope this helps.Infinite Recursion

Just to add to what Infinite Recursion gave you:
Writing to a file as Output will erase any existing data in the file. To add new text at the bottom of the file use the keyword Append instead of Output
Good Luck :) Hanan.

I would use this to get all of the information from a file:
Dim tmpString As String
Open FILENAME For Input As #1
tmpString = Input(LOF(1), #1)
Close #1
-Burbble

i think you can also use
dim filenum as integer, inputString as string
filenum = freefile
open filename for binary as #filenum
i think you can also use the "get" and "put" commands, but i think you have to include something first...
also, in binary mode, you can use the input command and i think print, but im not sure.
but any of the other above things work fine, just another way of doing things.eaw8806

If you just want to manipulate text files you can use the FileSystemObject by adding a reference to Microsoft Scripting Runtime library(scrrun.dll)
Dim s As New FileSystemObject
Dim s1 As TextStream
s.CreateTextFile "c:\sa.txt"
s.OpenTextFile "c:\sa.txt", ForWriting, True
Set s1 = s.OpenTextFile("c:\sa.txt", ForWriting, True)
s1.WriteLine "this is a line"
s1.Close
s.OpenTextFile "c:\sa.txt", ForReading, False
Set s1 = s.OpenTextFile("c:\sa.txt", ForReading, True)
MsgBox s1.ReadLine

In Binary mode you would do this:
Open FILENAME for Binary as #1
Get #1, , tmpString
Close #1Open FILENAME for Binary as #1
Put #1, , tmpString
Close #1

With all of the responces I have a question ... what's the fastest way? I'm looking to import and export large amounts of data from my program and looking for the fastest. So what would be the fastest route ... binary read/write, regular read/write or using the FileStream format ... or is there a fourth way that's faster than them all?
Shuesty

the problem with inputing large amounts of data to a file is that you have to store it. you cant do it all in once, if you input it to a string for example, because it can only hold a certin amount of data. if you did it in segments, and put each segment into a rtf or something, then it could work. (text boxes only store about 32KB). hope that helps
eaw8806

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |