473,785 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Pe ek() >= 0)
{
raw[i] = reader.ReadLine ();
++i;
}

// remove delimiter data
string[] data_arraySize = data[0].Split( new char [] { ' ' } );
string[] data_intintChar 1 = data[1].Split( new char [] { ' ' } );
string[] data_charArray1 = data[2];
string[] data_intintChar 2 = 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 10927
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.Pe ek() >= 0)
{
raw[i] = reader.ReadLine ();
++i;
}

// remove delimiter data
string[] data_arraySize = data[0].Split( new char [] { ' ' } );
string[] data_intintChar 1 = data[1].Split( new char [] { ' ' } );
string[] data_charArray1 = data[2];
string[] data_intintChar 2 = 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
4898
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, Pujo
12
3880
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
4
17878
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 problem is the charset? How I can avoid this warning? But the worst thing isn't the warning, but that the program doesn't work! The program execute all other operations well, but it don't print the converted letters: for example, in the string...
1
1391
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 by many threads at the same time. What sort of things that I need to do to make sure the methods and the class are multi-thread safe? I thought I don't need to do anything but I might be wrong. For example, I remember reading from some help file...
2
7656
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 Enter() the critical section that would cause a deadlock logs the complete deadlock loop (thread / Critical section) and raises an exception. It has helped us a lot in the past. However, to prevent further deadlocks, and to get a higher...
8
2944
by: Mr. SweatyFinger | last post by:
where have they all gone. alt.suicide? alt.findContol.then.slit.your.own.throat?
0
2329
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 Systems (MuCoCoS'08) Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08. <http://www.par.univie.ac.at/~pllana/mucocos08> *********************************************************************** Context
7
7260
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. Thread B will read data from Q to process; this processing will communicate with a website. I think the best Q architecture is ring buffer as large as possible.
5
7460
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 wouldn't work but it seems like maybe a read could coexist with append? Thanks, Bob
2
1851
by: beenanic | last post by:
How I can read a parameter from a multi-part form?
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.