English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Is there a way to read a text file from the bottom up in Visual Basic?

2007-07-16 08:24:48 · 2 answers · asked by DrDebate 4 in Computers & Internet Programming & Design

2 answers

You can load the textfile into either an array or collection. Once this is done you can do what ever you want by reading the data from the array or collection.

2007-07-16 08:54:21 · answer #1 · answered by MarkG 7 · 0 0

Forget about reading it into an array, and then accessing each element in reverse. A quick and dirty way is to simply read the entire file into a String object—all in one shot (VB.NEt's StreamReader.ReadToEnd), and then use the strRev() function on the String object.

If you do insist on actually reading the text file in reverse then, you can accomplish the task. However, it would be quite a bit of work.

The VB 6 objects are still compatible with VB.NET. I use VB 6 example below. However, you can research whether the VB.NET StreamReader object has comparable methods.

The TextStream object has methods such as ReadLine(), SkipCharacter() and SkipLine(), so, you can take advantage of these—in conjunction with the InStr() function to read a text file in reverse. This process is rather convoluted, in that you will have to set a keep track of a few counting variables, and use several loops in order to accomplish the task. The process involves tracking the number and location of each space on each line.

For each particular line, you record the following information. The number and position of each space. This information is recorded in an array.

After the entire text file has been processed, you close the text file.

Then, you would re-open the text file, and re-read it. You now know how many lines are in the text file, how may spaces are on each particular line, and their locations on that line. This time however, you using the already recorded information, skip to the last space, on the last line. You read that particular word. Next you read the word before it, and so on, until you have reached the first word on that line.

When you have finished reading a particular line then, you proceed to the previous line, and begin the process all over again—until you have finally processed the first line.

At the end, close the text file.

2007-07-16 17:29:49 · answer #2 · answered by Einstein 5 · 0 0

fedest.com, questions and answers