473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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[i] = 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!!!
Nov 16 '05 #1
2 10906
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.
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[i] = 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!!!

Nov 16 '05 #2
Kaede,
This topic has been well addressed in the .Net community. Please take
some time to read at least one of the following articles:
http://www.codeproject.com/cs/database/asc2xxx.asp
http://www.ftponline.com/vsm/2004_11...efault_pf.aspx

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
1
by: Zeng | last post by:
Hello, If I have an object that exposes many methods which don't change anything about the object (at least I'm not aware in terms of implementation of the ..net class). The methods are called...
2
by: Jürgen Devlieghere | last post by:
Hi, We are creating event-driven multi-threaded applications on a daily basis. To help us solving deadlocks, we implemented a CriticalSection class that does dead-lock detection: an attempt to...
8
by: Mr. SweatyFinger | last post by:
where have they all gone. alt.suicide? alt.findContol.then.slit.your.own.throat?
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
7
by: Boki | last post by:
Multi-thread read/write to a single file. I have two processing threads, thread A and thread B; and I called my queue as Q. Thread A will feed data into Q by user input, the timing is random....
5
by: Bob | last post by:
Hi, Is there any setting that I can use to allow the same file to be open in a filestream in append mode with one process and read in another. I can see why concurrent appends or write modes...
2
by: beenanic | last post by:
How I can read a parameter from a multi-part form?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.