Results 1 to 7 of 7

Thread: How to Read and Write to text files.......?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    84
    What would the code look like if I was trying to do something like this:

    -----
    //file1.txt

    TITLE1
    hello
    TITLE2
    Goodbye
    ------

    1. IF A = 1 THEN
    Read what's under TITLE1 (only) into a STRING
    ELSEIF A > 1 THEN
    Read what's under TITLE2 (only) into a STRING
    END IF

    2. Write stored string to File2.txt


    Thanks for any help

  2. #2
    Guest

    Talking

    load strings from file...
    Code:
    dim f as integer
    '...
    
    f = freefile
    open "File1.txt" for input as #f
    
    If A = 1 then
    line input #f, MyString 'now contains "TITLE1" in this case
    elseif A > 1 then
    line input #f, Mystring
    line input #f, Mystring
    line input #f, Mystring ' this will get the contents of the third line of the file (you could do this bit in a loop but this demonstration is less confusing
    end if
    
    close #f
    to write back to another file...

    Code:
    dim f as integer
    '...
    
    f = freefile
    open "File2.txt" for output as #f 'note: output will overwrite if the file already exists, to add to the end of the existing file use APPEND instead
    print #f, Mystring
    close #f






    [Edited by wossname on 10-22-2000 at 12:54 PM]

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    In order to do this you should write and read from ini files.Use the WritePrivateProfileString and GetPrivateProfileString API's.
    Code:
    WritePrivateProfileString "Titles", "Title1", "sometext", "C:\myinf.inf"
    
    Dim datab As String 'recieves text
    datab = Space(255)
    GetPrivateProfileString "Titles", "Title1", "Error", datab, 255, "C:\vl.inf"
    I am become death, the destroyer of worlds.
    mail:vlatkovr@hotmail.com

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Guest
    Vlatko, pointless if the file changes from day to day. Besides I think API functions are a bit advanced for Hambone at the moment (not meaning to be patronising, but there it is )

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    84
    Thanks......

    Still a little lost though. Let me ask it a different way.

    what if I have A1, A2, A3, A4, (etc.) as variables (an array?).

    I have a text file list (that needs to be a text file), but I don't know how long the list will be in the text file and I need each item in the text file to be placed into a variable (and I need to know how many items are there).

    like:
    --------
    //text1.txt
    apple
    orange
    bannana
    (etc)
    --------

    (ack) - then I need to display the items found in a list box to where the user can click an arror button to move selected items into another list box. The contents of the second list box (with the items the user moved into it)... need to be written to a new text file.

    I really appreciate the help - I'm almost completely green!

  6. #6
    Guest
    So basically, you want to load each line of a Text file into an Array, then into a ListBox?

    If so, try this example. Add the following to a Form with a ListBox and a CommandButton. (Change 'MyFile' to the name of the file you want to load).
    Code:
    Dim MyVar() As String
    Dim iCount As Integer
    
    Open "MyFile.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, tmp
        'Add items to Array
        ReDim Preserve MyVar(iCount)
        MyVar(iCount) = tmp
        iCount = iCount + 1
    Loop
    
    'Add items to the ListBox
    For I = LBound(MyVar) To UBound(MyVar)
        List1.AddItem MyVar(I)
    Next I

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    84
    Thanks everyone

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