sorting a record in text file

Posted by Brainothri under C# on 5/17/2010 | Views : 2795 | Status : [Member] | Replies : 2
I have a text file with the following data.

1,100,andrew,systemanalyst,20042010
2,100,virtusa,ssoftwareengineer,20042010
3,300,xavier,softwareengineer,20042010
4,111,sssd,manager,2004201

i want to search particular record using the second column .. say if i put 100 it should fetch the first record and second record.

if in ado.net so many methods are there. but using a text file and searching a record can anyone help me please.




Responses

Posted by: Vuyiswamb on: 5/18/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
well io is really different from ado.net. I think it will not be a good idea to keep on search textfile like this, that is why the databases were created because it was a mess trying to work with textfiles, they have limitation. i used to do something like this before my old applications on the linux environment, i will create my row terminator as a semicolon and used a comma for column terminator and loop through the textfile and search for the number or word and go to the first line of the row and copy from there till end end of the row which is terminated by a semicolon, but it becames a problem when you are searching for 100 and your data looks like this

1,100 ,andrew,systemanalyst,20042010
2,100,virtusa,ssoftwareengineer,20042010
3,300,xavier,softwareengineer,20042010
4,1100 ,sssd,manager,2004201


The last row will have 1100 and it will pick it up and it might be a problem. One thing that you need to note again is that this line

1,100,andrew,systemanalyst,20042010 


is not the same as

1, 100 ,andrew,systemanalyst,20042010 


In the First Example you will not Find 100 if you search for it. Because it has comas between it, the problem is not nessesarly a comma, but

1,100,


is read as one work , but if you use the second one , you will find the 100 because it space and the search can find the characters 100. This is time consuming and not worth doing. The best way is to use the database, there are a lot of things that text files shouldnt do and same for databases.

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Brainothri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sipra on: 5/19/2010 [Member] Starter

Up
0
Down
Hi,

For searching records in Text Files is it the criteria to search in with ADO.NET.
If not below is the solution to search from Text Files.

 

List<string> lines = new List<string>();
string sLine;
using (System.IO.StreamReader sr = new System.IO.StreamReader(@"D:\TextFile.txt"))
{
while ((sLine = sr.ReadLine()) != null && sLine.Contains("100"))
{
lines.Add(sLine);
}
}

foreach (string line in lines)
{
Response.Write(line);
}


Hope this helps.

Sipra

Brainothri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response