TheSwamp
Code Red => .NET => Topic started by: C. Alan 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.
-
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 >
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.
-
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.
-
Here's something to think about ...
Using this data file ;
FileLine 1
FileLine 2
FileLine 3
FileLine 4
FileLine 5
FileLine 6
and this code :
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 ;
FileLine 1
FileLine 2
Test 02
FileLine 3
FileLine 4
FileLine 5
FileLine 6
Test 01
and this is the debug piccy ;