-
October 9th, 2002, 02:10 PM
#1
writing / reading text files
Hi I just want to know how to write to a .txt file, I don't really care if it's ansi or unicode format, as long as I can save text to it.
And I want to edit normal .txt files but with a different extension (.rul).
I couldn't find a good example code on the net and for some reason my search on this forum keeps exceeding the maximum amount of time (30 secs.)
So I'm sorry if someone already posted an answer somewhere on this forum.
«(?¿|ßµrÑ3R|¿?)»
Operating System: Windows XP Pro
Programming Lang: Visual Basic 6.0
-
October 9th, 2002, 02:35 PM
#2
Code:
Option Explicit
'Set a reference to Microsoft Scripting Runtime then this
'Will work:
Private Sub Command1_Click()
Dim fs As FileSystemObject
Dim ts As TextStream
Set fs = New FileSystemObject
'To write
Set ts = fs.OpenTextFile("C:\mytestfile.txt", ForWriting, True)
ts.WriteLine "blah blah blah line1"
ts.WriteLine "blah bla bla blah line2"
ts.Close
'To Read
If fs.FileExists("C:\mytestfile.txt") Then
Set ts = fs.OpenTextFile("C:\mytestfile.txt")
Do While Not ts.AtEndOfStream
MsgBox ts.ReadLine
Loop
ts.Close
End If
Set ts = Nothing
Set fs = Nothing
End Sub
Last edited by Cimperiali; November 11th, 2003 at 07:22 AM.
-
October 9th, 2002, 03:06 PM
#3
Thanks for the help, the code seems to work fine.
«(?¿|ßµrÑ3R|¿?)»
Operating System: Windows XP Pro
Programming Lang: Visual Basic 6.0
-
October 9th, 2002, 08:06 PM
#4
And how to read the characters inside the .txt? Example, count all the "a"s from the file?
I Don´t know you, God Knows!!
-
December 30th, 2005, 05:19 AM
#5
Re: writing / reading text files
How to save data in the text file as binary code. Reply as soon as possible(urgent)
thanx
sateesh
-
December 30th, 2005, 05:38 AM
#6
Re: writing / reading text files
Originally Posted by sateesh_raps
How to save data in the text file as binary code. Reply as soon as possible(urgent)
thanx
sateesh
Here is an example from MSDN
Code:
Private Type Record ' Define user-defined type.
ID As Integer
Name As String * 20
End Type
Private Function writeToFile()
Dim MyRecord As Record, RecordNumber ' Declare variables.
' Open file for random access.
Open "C:\TESTFILE" For Random As #1 Len = Len(MyRecord)
For RecordNumber = 1 To 5 ' Loop 5 times.
MyRecord.ID = RecordNumber ' Define ID.
MyRecord.Name = "My Name" & RecordNumber ' Create a string.
Put #1, RecordNumber, MyRecord ' Write record to file.
Next RecordNumber
Close #1 ' Close file.
End Function
Private Function readFromFile()
Dim MyRecord As Record, Position ' Declare variables.
' Open sample file for random access.
Open "C:\TESTFILE" For Random As #1 Len = Len(MyRecord)
' Read the sample file using the Get statement.
Position = 3 ' Define record number.
Get #1, Position, MyRecord ' Read third record.
Close #1 ' Close file.
Debug.Print MyRecord.ID
Debug.Print MyRecord.Name
End Function
Public Sub Main()
writeToFile
readFromFile
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|