473,480 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to parse a .txt file in C#

Hi all,

I have to parse a .txt file and retrieve only "Name" from second
column and store it in a list

My text file is of this format:

Bit Name Description
-1 Lucky groupa
-1 Roy groupb

In the list i should have only "Lucky" and "Roy" and nothing else.

I am very new to C# can anyone please tellme how i can do this

thanks for help,
Nithin
I
Nov 4 '08 #1
2 18620


<ni************@gmail.comwrote in message
news:b1**********************************@x16g2000 prn.googlegroups.com...
Hi all,

I have to parse a .txt file and retrieve only "Name" from second
column and store it in a list

My text file is of this format:

Bit Name Description
-1 Lucky groupa
-1 Roy groupb

In the list i should have only "Lucky" and "Roy" and nothing else.

I am very new to C# can anyone please tellme how i can do this

thanks for help,
Nithin
I
This is actually pretty simple depending, but we need a little more
information. Is Bit always a -1 or a 0 or...always an integral value? Is
Name always a single word? Is Name ever surrounded with quotes? Could
there be any types of delimiters besides a space?

From the simple example you gave, you simply have 3 columns delimited by a
space...:

If you are using .Net 3.5, you can make use of anonymous types and LINQ
(note, I know this is definitely not the most efficient block of code in the
world, but hey, it works :)

string[] lines = File.ReadAllLines(@"C:\input.txt");
int i = 0;
var items =
from line in lines
where i++ != 0
select new {
Bit = line.Split(new char[] { ' ' }, 3)[0],
Name = line.Split(new char[] { ' ' }, 3)[1],
Description = line.Split(new char[] { ' ' }, 3)[2]
};

foreach (var item in items) {
Console.WriteLine(
"Bit: {0}, Name: {1}, Description: {2}",
item.Bit,
item.Name,
item.Description
);
}

// The following can be used in version 2.0 and does not make use of
anonymous types nor LINQ:

string[] lines = File.ReadAllLines(@"C:\input.txt");
int i = 0;

foreach (string line in lines) {
if (i++ == 0) continue;

string[] items = line.Split(new char[] { ' ' }, 3);
Console.WriteLine(
"Bit: {0}, Name: {1}, Description: {2}",
items[0], items[1], items[2]
);
}
HTH,
Mythran
Nov 4 '08 #2
StreamReader reader = new StreamReader(filePath);
string line;

//Assume that is a tab, if space change to " ".ToCharArray()
char[] splitChar = "\t".ToCharArray();

while(null != (line = reader.ReadLine))
{
string[] split = line.Split(splitChar);

int bit= int.Parse(split[0]);
string name = split[1];
string description = split[2];

//Do something with parse here?
}

reader.Dispose();

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
<ni************@gmail.comwrote in message
news:b1**********************************@x16g2000 prn.googlegroups.com...
Hi all,

I have to parse a .txt file and retrieve only "Name" from second
column and store it in a list

My text file is of this format:

Bit Name Description
-1 Lucky groupa
-1 Roy groupb

In the list i should have only "Lucky" and "Roy" and nothing else.

I am very new to C# can anyone please tellme how i can do this

thanks for help,
Nithin
I
Nov 5 '08 #3

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

Similar topics

1
2381
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
22
872
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
3
2634
by: slylos | last post by:
I've got a section of code in my app that keeps track of how much time an employee has accrued, which equals out to 1.33 days of PTO per month. I'm trying to store the time accrued so far in an...
6
8533
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
5
64572
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
63973
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
11
3489
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
2
3206
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString",...
6
2957
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hello, I have an XML file generated from a third party application that I would like to parse. Ideally, I plan on having a windows service setup to scan various folders for XML files and parse the...
5
2678
by: goldtech | last post by:
SAX XML Parse Python error message Hi, My first attempt at SAX, but have an error message I need help with. I cite the error message, code, and xml below. Be grateful if anyone can tell me...
0
7040
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
6905
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
7041
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,...
1
6736
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
6908
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
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
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 ...

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.