Results 1 to 4 of 4

Thread: How do I...?

  1. #1

    Thread Starter
    Member Cahlel's Avatar
    Join Date
    Aug 2000
    Location
    Earth (I think)
    Posts
    63

    Exclamation

    How do I open a file for input, output or to append, I know you use
    open [filename] for [input/output/append] as #[#]
    but how do you write to a file and read from a file?
    please help me....

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'writing or reading data to or from a text file
    'open for append adds the data to the end of file
    'open for input opens the file for read only
    'open for output opens the file and overwrites the file
    '
    '
    Private Sub Command1_Click()
    
    'this is the number of your file
    Dim intFilenum As Integer
    
    'set it to the next available free number
    intFilenum = FreeFile
    
    'this is your filename
    Dim strFile As String
    strFile = "A:\myfile.txt"
    
    'this is the text you want to save (textbox)
    Dim strText As String
    strText = Text1.Text
    
    'open the file for writing to as next freefile number
    
    Open strFile For Append As intFilenum  'see above options
    'write to the file
        Write #intFilenum, strText  'to read use  [input #filenum,strtext]
    
    'close the file
    Close intFilenum
        
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Guest
    To Write to a file:
    Code:
    Open "MyFile.txt" For Output As #1
    Print #1, "Hello"
    Close #1
    To Read a file:
    Code:
    Open "MyFile.txt" For Input As #1
    Input #1, MyVar
    Close #1
    
    'Display the result
    MsgBox (MyVar)

  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    There are many different ways.

    Input opens a file strictly for input.
    Output opens a file, and overwrites whatevre is there.
    Append opens a afile and places new data on the end of the file.

    Code:
    'INPUT METHODS.
    
    'inpute a Line
    Line Input #1, strLine
    
    'inputs a set amount of characters. In this cse the whole file
    strFile = Input(lof(fileNum), #fileNum)
    
    'inputs a number of variables from a comma delimted file
    Input #1, var1, var2, var3
    
    
    'OUTPUT METHODS.
    
    'Prints text as is.
    Print #1, strText
    
    'Inserts commas between variables, and places " around strings.
    Write #1, Var1, Var2
    There is also the Get / Put statemnets that are used on Binary / Random file modes.
    Iain, thats with an i by the way!

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