How To Read/Write File in VB6

Score
0
Vote Up
Carven July 9, 2003 at 01:41:25 Pacific
Specs: Win2000, P4 2.4

Can anyone give example of reading text file and write to it?


Reply ↓  Report •


#1
Vote Down
Score
18
Vote Up
July 9, 2003 at 02:23:30 Pacific

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 String

Open "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


Reply ↓  Report •

#2
Vote Down
Score
3
Vote Up
Hanan July 9, 2003 at 11:37:42 Pacific

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.


Reply ↓  Report •

#3
Vote Down
Score
1
Vote Up
Burbble July 12, 2003 at 09:19:20 Pacific

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


Reply ↓  Report •

#4
Vote Down
Score
5
Vote Up
eaw8806 July 13, 2003 at 20:02:55 Pacific

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



Reply ↓  Report •

Related Posts

#5
Vote Down
Score
11
Vote Up
tvssarma July 14, 2003 at 02:18:11 Pacific

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


Reply ↓  Report •

#6
Vote Down
Score
2
Vote Up
Burbble July 14, 2003 at 06:16:21 Pacific

In Binary mode you would do this:

Open FILENAME for Binary as #1
Get #1, , tmpString
Close #1

Open FILENAME for Binary as #1
Put #1, , tmpString
Close #1


Reply ↓  Report •

#7
Vote Down
Score
0
Vote Up
Shuesty July 14, 2003 at 06:24:55 Pacific

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


Reply ↓  Report •

#8
Vote Down
Score
0
Vote Up
eaw8806 July 14, 2003 at 10:05:37 Pacific

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


Reply ↓  Report •

Reply to Message Icon Start New Discussion
« linking c application CLass inheritance in C++ »

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

Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC