Input and Line Input
A simple text file has each entry separated by a line feed and/or carriage return. Opening in notepad would show something similar to
DocA comma deliminated file has entries that are separated by commas. If the above text file were written as a comma deliminated file, opening in Notepad would show
Happy
Grumpy
Sleepy
Sneezy
Bashful
Dopey
Doc, Happy, Grumpy, Sleepy, Sneezy, Bashful, Dopey
For this demo, a small comma deliminated file must first be created. Copy, paste and run this code. The execution of this code will create (write) the file
dwarves.dat
'Write a comma deliminated file
Open "dwarves.dat" for Output as #1
Print #1, "Doc, Happy, Grumpy, Sleepy, Sneezy, Bashful, Dopey"
Close #1
Now that a file exists for this demo, the file can be opened and read. First, open the file and count how many items are present.
'Count the number of items
nDwarves = 0
Open "dwarves.dat" for Input as #1
While eof(#1) = 0
Input #1, dwarf$
nDwarves = nDwarves + 1
Wend
Close #1
Print "There are ";nDwarves;" dwarves."
A word about EOF: EOF means End Of File. In computer terms, 0 = False and 1 = True. So, the line
While eof(#1) = 0
means while the end of the file is false (or hasn't been reached), keep looping and reading more.
Having counted the number of items, a For Next Loop can be used to place each item in an array. The array must first be dimmed.
'Dim the array
Dim dwarf$(nDwarves)
'Read the file again, this time filling your array
Open "dwarves.dat" for Input as #1
For i = 1 to nDwarves
Input #1, dwarf$(i)
Next i
Close #1
Print the array to the Mainwindow (console) to see each array element.
'Print the array
For i = 1 to nDwarves
Print dwarf$(i)
Next i
Some files group related items, or a subset, with commas and then separate each subset from the next with a line feed and/or carriage return. This next demo writes such a file.
'Write comma deliminated data with line feeds
Open "persons.dat" for Output as #1
Print #1, "Jack, Jill"
Print #1, "Hansel, Gretel"
Print #1, "Cinderella, Prince Charming"
Print #1, "Napolean, Antoinette"
Close #1
Using the same procedure as above, the number of items can be counted
'Count the number of items
nPersons = 0
Open "persons.dat" for Input as #1
While eof(#1) = 0
Input #1, person$
nPersons = nPersons + 1
Wend
Close #1
Print "There are ";nPersons;" persons."
the array dimmed
'Dim the array
Dim persons$(nPersons)
and the array filled with the individual elements
'Fill the array
Open "couples.dat" for Input as #1
For i = 1 to nPersons
Input #1, person$(i)
Next i
Close #1
Print this array.
'Print the array
For i = 1 to nPersons
Print person$(i)
Next i
Well, hey! What happened to our couples? Everybody's a single. That's not right. To preserve the subsets, use the Line Input command. The Line Input command inputs all text up to the line feed. This will keep the subsets, or couples in this case, together. Again, the number of elements must first be counted, but this time with Line Input.
'Count the number of items
nCouples = 0
Open "persons.dat" for Input as #1
While eof(#1) = 0
Line Input #1, couple$
nCouples = nCouples + 1
Wend
Close #1
Print "There are ";nCouples;" couples."
Dim the array. Notice there are only 4 elements now rather than 8. This is because each array holds a string with 2 persons.
Dim couple$(nCouples)
Read the text file again, this time using a For Next loop.
'Fill the array
Open "persons.dat" for Input as #1
For i = 1 to nCouples
Line Input #1, couple$(i)
Next i
Close #1
Once again, print this array.
'Print the array
For i = 1 to nCouples
Print couple$(i)
Next i
Sometimes you'll want to break up even the best couple. Use the Word$ command to retrieve the separate elements. Word$ can be given an optional deliminator for separation.
Word$("Robin Hood, Maid Marian", 1) yields "Robin" but Word$("Robin Hood, Maid Marian", 1, ",") yields "Robin Hood" and Word$("Robin Hood, Maid Marian, 2, ",") yields "Maid Marian".
Print your array out using Word$
'Use Word$ with optional comma (",") deliminator to separate names
For i = 1 to nCouples
Print Word$(couple$(i), 1, ","), Word$(couple$(i), 2, ",")
Next i
Using Word$ with the comma deliminator allows you to retrieve the separate items in the subset array.
The Input and Line Input commands are not the only way to retrieve the contents of a file, but these commands are the most commonly used when dealing with a simple text file or a comma deliminated file