Results 1 to 2 of 2

Thread: VB - File I/O

Threaded View

  1. #1

    Thread Starter
    Lively Member Algar's Avatar
    Join Date
    Jun 2003
    Location
    A place that never existed
    Posts
    127

    VB - File I/O

    I've made a file input output tutorial and was wondering if some of you guys could comment on it, tell me if its good or not.

    VB Code:
    1. 'This tutorial explains how to
    2. 'read files, and write them into
    3. 'variables and then also to add
    4. 'to files. Quite simple, and if
    5. 'you have any questions then
    6. 'private message Skateboarder.
    7.  
    8. Option Explicit 'You should know
    9.                 'what this means,
    10.                 'all variables used
    11.                 'must be declared.
    12.    
    13. Dim File As String 'This is the file we are going to open
    14.                    'right now, we must specify it in Form_Load
    15.  
    16. Private Sub Command1_Click()
    17.     File = InputBox$("What is the file name?")
    18.     'Don't mess this up unless you want to learn
    19.     'the errors that will be given
    20.                                
    21. End Sub
    22.  
    23. Private Sub Command2_Click()
    24.     If MsgBox("Alright, so we're writing to " & File & ", correct?", vbYesNo) = vbYes Then
    25.         Open "C:\FileIO.txt" For Append As #1 'Remember, Append means add to
    26.             Print #1, txtFileString
    27.         Close #1
    28.     End If
    29. End Sub
    30.  
    31. Private Sub Command3_Click()
    32.    
    33.     Dim strVar As String 'We need a string to
    34.                          'write to
    35.                        
    36.     'Now we begin the reading part
    37.     Open File For Input As #1 'Now this is something new
    38.                               'Input means to read the file
    39.                               'Now, if there was no
    40.                               '"C:\FileIO.txt" file then
    41.                               'an error would be generated
    42.                               'File Not Found would be
    43.                               'the error
    44.     Do Until EOF(1) 'EOF is a boolean meaning End Of File
    45.                     '1 is the number since we open
    46.                     'it For Input As #1
    47.                     'EOF(1) is true when we reach the
    48.                     'end of the file
    49.         Line Input #1, strVar 'We read the line
    50.                               'and that line is a
    51.                               'string, so we
    52.                               'put it into a string
    53.         'Right here we could put some code checking
    54.         'the contents of that string
    55.         'For now we'll use MsgBox and that will show us
    56.         'the contents of the text file
    57.         MsgBox strVar 'Now read the MsgBox
    58.     Loop
    59.    
    60.     Close #1 'Close it up now, so we don't get
    61.              'a File Already Open error the next
    62.              'time we try to open it
    63. End Sub
    64.  
    65. Private Sub Command4_Click()
    66.     Dim FileStr As String
    67.    
    68.     Open File For Input As #1 'This should be familiar
    69.                               'check out Command3_Click
    70.         FileStr = Input$(LOF(1), 1)
    71.         'Now let's explain this argument by argument
    72.         'Input$ - look it up in the object browser
    73.         'The first argument we pass is the len
    74.         'LOF is Length of File and 1 is our filenumber
    75.         'because that's what we open the file
    76.         'For Input As. our second argument tells
    77.         'the file to read from, and 1 is our
    78.         'filenumber, so let's tell them one.
    79.        
    80.         'let's see this file now!
    81.         MsgBox FileStr
    82.     Close #1
    83. End Sub
    84.  
    85. Private Sub Form_Load()
    86.     'First, we must make sure
    87.     'that we have a file to
    88.     'read from, so we check
    89.     'if the file is there
    90.     'using Dir$
    91.      
    92.     File = "C:\FileIO.txt"
    93.     If Dir$(File) = "" Then 'Check if "C:\FileIO.txt" is created
    94.         Debug.Print "No tutorial file found, so it will be created."
    95.         Open File For Output As #1 'For output means we create
    96.         Close #1                              'the file and write to it,
    97.     End If                                    'or, if it exists
    98.                                               '(which it doesn't because we
    99.                                               'checked it with Dir$)
    100.                                               'we clear it and write whatever we tell
    101.                                               'VB to write to it
    102.        
    103.        
    104.     'Now, we know we have a file to work with,
    105.     'so let's start writing to it
    106.    
    107.     Open File For Append As #1            'Append means to add to the file
    108.                                           'On the other hand, For Output clears the
    109.                                           'file/if not there creates it, and
    110.                                           'writes to it from scratch whether it's
    111.                                           'already been made or not
    112.    
    113.     Print #1, "Hello" 'Print means writing to it
    114.                       'and since we specified
    115.                       'For Append As #1, we use
    116.                       'Print #1, this also
    117.                       'applies when opening
    118.                       'the file For Output As #1
    119.                      
    120.                       'Tip: Whenever writing
    121.                       'to a file using Print
    122.                      
    123.     Close #1 'Close it up now, so we don't get
    124.              'a File Already Open error the next
    125.              'time we try to open it
    126.              
    127.              
    128.              
    129.     'At this point in the tutorial you should
    130.     'understand what two ways to write to
    131.     'a file, using Append or Output
    132. End Sub

    ~B-Rabbit
    Attached Files Attached Files

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