?
Solved

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

Posted on 2003-02-25
6
Medium Priority
?
204 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:lionheart620
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Accepted Solution

by:
Esopo earned 200 total points
ID: 8021273
Hi,

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)
   J=J+1
Wend

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


0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8022124
a very nice function of visual basic is the

app.path

dim Filename as string

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

Open Filename for output as #1

Then use above stuff.

Jacamar
0
 
LVL 1

Expert Comment

by:wehoit
ID: 8023242
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
fNum=FreeFile
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...
myStr=Replace$(myStr,chr(34),"")

...and then split the string to get the elements...
myVar=split(myStr,",")

OR

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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Esopo
ID: 8029400
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...
myStr=Replace$(myStr,chr(34),"")
**********

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.
0
 
LVL 1

Author Comment

by:lionheart620
ID: 8038395
Thanks a lot, this is everything I need
0
 
LVL 1

Author Comment

by:lionheart620
ID: 8038396
Thanks a lot, this is everything I need
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question