Opening files, reading each line into an array. AND Writing files.

I want to create a .txt file that has multiple lines of information. I want to read each line as an element in an array so I can use them in my program. I also would like to know how to WRITE data to a .txt file, whether each piece of info is it's own string or an element of an array (whatever is best, probably an array).

Please when you explain, explain what is going on so I can manipulate it as needed. I don't know what LOF does either, so if someone could explain I'd appreciate it. If there is a better way to read a file into an array than line by line, or a different file type that is BETTER to use for reading stuff, then that would be useful too. Thanks
Who is Participating?
EsopoConnect With a Mentor Commented:

If you are new to file I/O (Input / output) I recommend you use something like this to store and read to a text file:

Dim FileNum
FileNum=Freefile  'Asign a valid free file number

 'Open the file for writing
Open "file.txt" for Output As FileNum

For J= Lbound(Array1) to Ubound(Array1)  'loop through the whole array
   Print #FileNum, Array1(J)
Next J

Close FileNum

This will open the file for writing, if it doesn't exist it will create it and if the file existed it will be overwritten; then store the array's contents into the file line by line using print; then finally it closes the File.

LOF retrieves the size in bytes of a file you have opened.

To read the file you just created with the code above, you can use something like this:

Dim FileNum
FileNum=Freefile  'Asign a valid free file number

 'Open the file for writing
Open "file.txt" for Input As FileNum
J=Ubound(Array1)  'The first position in the array, you could use 0 or 1 directly

While Not EOF(FileNum)
   Line input #FileNum, Array1(J)

Close FileNum

Notice this time the file has been open for INPUT, it also assumes your Array1 is big enough to hold the content of the file.
EOF() works like LOF() but it tells you if the File has reached the end.
'Line input' reads the following line everytime.

Best regards

a very nice function of visual basic is the


dim Filename as string

Filename = app.path + "\File.txt"

Open Filename for output as #1

Then use above stuff.

I would recommend using the WRITE statement if your array can be thought of like comma separated file.
(look for "Write #" in the Index of MSDN)

That is, if the data can look like this per line:
"value 1","value 2","value 3",<etc>

Each value written (string or numeric) gets quotes around it and a comma separator.

So, a single dimensional array of 5 elements could be written like this:

Dim fNum as integer
myFileName=App.path & "\mydata.txt"
Open myFileName for Output As fNum 'or open for Append
  Write #fNum, array(0),array(1),array(2),array(3),array(4)
Close fNum

Depending on the number of elements per line or what you will do with them, you can read them back in by line...
Line Input #fNum, myStr

...remove the quotes...

...and then split the string to get the elements...


Read them one at a time into your array...
for i=0 to ubound(myArray)
   Input #fNum, tmpVar
next i

Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

I do not agree with wehoit for a couple of things:

1. We don't know how many items the array has,
2. We donĀ“t know the contents of the array
3. for a single dimensional array, the print method generates both a smaller file and an easier read.

If you use quotes to separate values and then remove them:
...remove the quotes...

Your values will not be able to have quotes (they don't usually do, but there is no reason to stop them from using quotes anyway). Also,

If the values are sentences written by the user, they might have commas within them, so if you remove quotes and then separate the value by the commas created by the WRITE method, you would also be separating the sentences as if they were different entries.

If I were to choose a way of going, I'd stick to PRINT (even with multi dimensional arrays) or Databases if the array get's bigger than 10,000 entries.

Best regards.
lionheart620Author Commented:
Thanks a lot, this is everything I need
lionheart620Author Commented:
Thanks a lot, this is everything I need
All Courses

From novice to tech pro — start learning today.