How to read multi-line files in C# and parse the content probably

K

kaede

Hi all,

Given the following textfile (i.e test.txt)

5 5
5 2 E
aeiou
4 3 Y
abcdefghij

What is the best way to read the file in and parse the data to its appropriate type?

I was using the following method, but it looks really tedious and not efficient

// Read File
string[] raw = new string[5];
StreamReader reader = File.OpenText("test.txt");
int i = 0;
while(reader.Peek() >= 0)
{
raw = reader.ReadLine();
++i;
}

// remove delimiter data
string[] data_arraySize = data[0].Split( new char [] { ' ' } );
string[] data_intintChar1 = data[1].Split( new char [] { ' ' } );
string[] data_charArray1 = data[2];
string[] data_intintChar2 = data[3].Split( new char [] { ' ' } );
string[] data_charArray2 = data[4];

// for each data type do conversion
int x = data_arraySize[0].ToInt()
int y = data_arraySize[0].ToInt()

Any help would be appreciated!!!
 
B

ben

First off, if you want to parse a sequence of text the text must be written
under certain grammar, now design your gramma, then write your own lexical
analyzer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top