Computing.Net > Forums > Programming > How To Read/Write File in VB6

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How To Read/Write File in VB6

Reply to Message Icon

Name: Carven
Date: July 9, 2003 at 01:41:25 Pacific
OS: Win2000
CPU/Ram: P4 2.4
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: July 9, 2003 at 02:23:30 Pacific
Reply:

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


0

Response Number 2
Name: Hanan
Date: July 9, 2003 at 11:37:42 Pacific
Reply:

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.


0

Response Number 3
Name: Burbble
Date: July 12, 2003 at 09:19:20 Pacific
Reply:

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


0

Response Number 4
Name: eaw8806
Date: July 13, 2003 at 20:02:55 Pacific
Reply:

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



0

Response Number 5
Name: tvssarma
Date: July 14, 2003 at 02:18:11 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Burbble
Date: July 14, 2003 at 06:16:21 Pacific
Reply:

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


0

Response Number 7
Name: Shuesty
Date: July 14, 2003 at 06:24:55 Pacific
Reply:

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


0

Response Number 8
Name: eaw8806
Date: July 14, 2003 at 10:05:37 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How To Read/Write File in VB6

How to read/write data in UNIX Format (.NET) www.computing.net/answers/programming/how-to-readwrite-data-in-unix-format-net/19942.html

how to read xls file in delphi www.computing.net/answers/programming/how-to-read-xls-file-in-delphi/19316.html

vb read and write file www.computing.net/answers/programming/vb-read-and-write-file/6672.html