Author Topic: Text Readline only one way?  (Read 2674 times)

0 Members and 1 Guest are viewing this topic.

C. Alan

  • Guest
Text Readline only one way?
« on: October 23, 2006, 12:00:53 AM »
So I have been working with Microsoft Visual Basic 2005 Express to try to learn more VB for Autocad.

I have been writing a little application that takes a text file that is output from one of my Hydrology programs (civil design), and converts it into a form that can be used in my flood modeling software (Flo-2d).  So I have set up my application to use the readline procedure of the file stream class, and for the most part it works.  The text I need for my new file is not always in the same place, so I have to search the text line by line and use the Indexof string procedure to search each line for key words.

The problem I have right now is that it seems that the readline procedure only goes one way.  Is there a way to get the place holder that is used with readline to back up a line, or to return to the start of the file?  I tried using myStreamReader.BaseStream.Seek(0, IO.SeekOrigin.Begin) ,but it does not seem to affect where readline reads from.

Is there any way of getting readline to go up a line or two without closing and re-opening the file?

One last question.  Does readline return a -1 if the line is blank? Meaning it only has a CR character on it.
« Last Edit: October 23, 2006, 12:14:41 AM by C. Alan »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Text Readline only one way?
« Reply #1 on: October 27, 2006, 10:14:44 PM »
How big is the file, and how much processing do you want to do .. ?
Have you thought about reading the whole file into an ArrayList

something like < not tested > 
Code: [Select]
ArrayList theFileData = new ArrayList();
using (StreamReader myReader = new StreamReader("HydroData.txt"))
{
   while (myReader.Peek() > -1)
   {
      theFileData.Add(myReader.ReadLine());
   }
}

sorry ... don't do VB :-)


added: or perhaps have a look at System.IO.File.ReadAllLines , it may be a more salubrious solution.
« Last Edit: October 27, 2006, 10:20:49 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Troy Williams

  • Guest
Re: Text Readline only one way?
« Reply #2 on: October 28, 2006, 07:30:39 AM »
How about posting a sample of the file that you are reading and a sample of the code doing the reading. It would also be nice to see a sample of the output file.

As far as readline goes, I would go with Kerry's suggestion and use the ReadAllLines method. It will read the entire text file into a string variable. I do all of my text processing this way (from vb6 to vb 2005) because it is much easier to manipulate the text file when it is in one string.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Text Readline only one way?
« Reply #3 on: October 29, 2006, 04:38:57 AM »
Here's something to think about ...

Using this data file ;
Quote
FileLine 1
FileLine 2
FileLine 3
FileLine 4
FileLine 5
FileLine 6
and this code :
Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication732
{
    class Program
    {
        /// <summary>
        /// codehimbelonga kwb@theswamp 20061029
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string qualifiedFileName = @"K:\TestData20061029.txt";
            if (File.Exists(qualifiedFileName))
            {               
                List<string> list = new List<string>(File.ReadAllLines(qualifiedFileName));
                list.Insert(6, " Test 01");
                list.Insert(2, " Test 02");
                Console.WriteLine(list[7]);
                Console.ReadKey();

                File.WriteAllLines(qualifiedFileName, list.ToArray());
            }
        }
    }
}

This is the resulting file ;
Quote
FileLine 1
FileLine 2
 Test 02
FileLine 3
FileLine 4
FileLine 5
FileLine 6
 Test 01
and this is the debug piccy ;
« Last Edit: October 29, 2006, 04:42:46 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.