Click here to Skip to main content
15,891,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
I am working in C# windows application where I have to read a text file where so many rows are there and every field is separated by tab key. Now my job is to find the particular row by a unique first_name string value and show all the related data of the that particular row.

id first_name Last_Name age height address
001 ABC dsfs 12 6.2 dfdfrewfewrewrer
034 sdg dwqdew 32 8.4 wererfwrw
009 dfdf erewr 24 5.8 ewrdrefdrew

so if I select sdg (first_name) related data of the same should select
Posted
Updated 6-Mar-12 23:18pm
v2

1. Read the file contents File.ReadAllText
2. split the rows by using String.Replace("\r\n", "$$$").split(new char[]{'$$$'});
3. Now you have an array of string for each row of text file.
4. Split each row string using string.split('\t')
5. You will further get array of strings for each column value.

6. Now if you know the column wise location of your key string i.e [0] you can device carefully nested foreach statements do perform these splits and search.

Hope it will give you some idea.
 
Share this answer
 
Hi,

you could read in the whole text with

C#
System.IO.File.ReadAllText(@"PATH");


then proof if your search term has found and take the whole line.

But I'm asking me why you don't use xml schema for such a case?

Best Regards
 
Share this answer
 
There are a lot of different ways to do this, but the simplest is probably to read the text into an array of strings, and compare the IDs:
C#
string[] lines = File.ReadAllLines(@"D:\Temp\MyList.txt");
string match = "0321\t";
string wholeLine = "";
foreach (string line in lines)
    {
    if (line.StartsWith(match))
        {
        wholeLine = line;
        break;
        }
    }
 
Share this answer
 
Comments
IndrajitDasgupat 7-Mar-12 5:15am    
if it is selecting with first name which is unique then how can I do
OriginalGriff 7-Mar-12 5:23am    
Sorry?
If I understand you correctly, then see the "\t" on the end of the match string? That is the tab character, so the StartsWith needs to match the whole string, including the tab.
If I don't, then please try to explain in more detail.
IndrajitDasgupat 7-Mar-12 5:50am    
Sir I got it by using IndexOf
if(line.IndexOf(match)!=-1)
{
}
thanks for giving valuable hints

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900