CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Jun 2002
    Location
    Holland
    Posts
    54

    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

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    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.

  3. #3
    Join Date
    Dec 2005
    Posts
    1

    Re: writing / reading text files

    How to save data in the text file as binary code. Reply as soon as possible(urgent)


    thanx
    sateesh

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: writing / reading text files

    Quote 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

  5. #5
    Join Date
    Jun 2002
    Location
    Holland
    Posts
    54
    Thanks for the help, the code seems to work fine.
    «(?¿|ßµrÑ3R|¿?)»

    Operating System: Windows XP Pro
    Programming Lang: Visual Basic 6.0

  6. #6
    Join Date
    Oct 2002
    Location
    Curitiba
    Posts
    3
    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!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured