Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
How could i retrieve data from .txt file (Note: by expected order), mean that i want a specific data not all file not all line only word or more so i can use it in statistics in log system.
Posted
Updated 18-Oct-12 1:58am
v3
Comments
adriancs 18-Oct-12 7:27am    
You did not specify what kind of file. excel? csv?
and this:
mouse click, double click and move
is another question.
engmebeed 18-Oct-12 7:53am    
Sorry .txt file
Yes this is another question
adriancs 18-Oct-12 8:04am    
can u post some of the content inside the text file?
OriginalGriff 18-Oct-12 7:40am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
engmebeed 18-Oct-12 7:54am    
ok, sorry

 
Share this answer
 
Text files are not good at extracting a little bit here and there - they are really monolithic files inteneded to be read from beginning to end - they have no concept of "random access", or even of "lines" unless you code implements it.

So extracting a little bit of the text only is going to be a pain! There are two basic ways:
1) Read the whole file as lines, and process them individually
2) Read the whole file (or as chunks) and use a regex to identify and extract the areas you are interested in.

There are many other ways if the file content contains some organisation, such as XML or similar.

Which you want depends on what your file content looks like, and what kind of bits you want to extract.

Sorry to be horribly vague, but there really isn't a "one size fits all" solution to this.
 
Share this answer
 
Comments
CPallini 18-Oct-12 8:08am    
5.
Akinmade Bond 18-Oct-12 9:00am    
+5
Hi,

Here I am giving some code snipet using stream reader to retrive some information from a .txt file and binding it to a grid view, after assigning those information to a datatable.

<pre lang="c#"> using (FileStream s = new FileStream("C:/TextFileToGridView.txt", FileMode.Open))
        using (StreamReader r = new StreamReader(s))
        {
            List<Object> ds = new List();

            while (!r.EndOfStream)
            {
                String l = r.ReadLine();
                if (l[0] != '%')
                {
                    String p = l.Replace("   ", ";");
                    String[] a = p.Split(';');
                    if (a[0] != "")
                    {
                        ds.Add(new { c0 = a[0], c1 = a[1], c2 = a[2], c3 = a[3] });
                    }
                }
            }
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }


If you want any clarification please revert.

Thanks
tapan kumar
 
Share this answer
 
Comments
tarun kumar sahoo 18-Oct-12 10:38am    
hi thanks it helps me
fjdiewornncalwe 18-Oct-12 15:56pm    
My vote of 1: sock-puppet vote.
Kill the BUG 18-Oct-12 10:45am    
thanks for sharing this, Tapan
fjdiewornncalwe 18-Oct-12 16:03pm    
Another sock puppet.

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