Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
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 18 Oct '12 - 1:23
Edited 18 Oct '12 - 1:58

Comments
adriancs - 18 Oct '12 - 7:27
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:53
Sorry .txt file Yes this is another question
adriancs - 18 Oct '12 - 8:04
can u post some of the content inside the text file?
OriginalGriff - 18 Oct '12 - 7:40
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:54
ok, sorry
OriginalGriff - 18 Oct '12 - 7:57
It's ok - all you have to do is remember that we only get to know what you tell us. So if you don't tell us much, we have to guess the rest, and we hate guessing! :laugh:
engmebeed - 18 Oct '12 - 7:59
Thanks for reply

3 solutions

  Permalink  
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.
  Permalink  
Comments
CPallini - 18 Oct '12 - 8:08
5.
geekbond - 18 Oct '12 - 9:00
+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
  Permalink  
Comments
tarun kumar sahoo - 18 Oct '12 - 10:38
hi thanks it helps me
Marcus Kramer - 18 Oct '12 - 15:56
My vote of 1: sock-puppet vote.
Kill the BUG - 18 Oct '12 - 10:45
thanks for sharing this, Tapan
Marcus Kramer - 18 Oct '12 - 16:03
Another sock puppet.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 239
1 OriginalGriff 173
2 Maciej Los 135
3 Mahesh Bailwal 104
4 Aarti Meswania 98
0 Sergey Alexandrovich Kryukov 10,214
1 OriginalGriff 7,819
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 18 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid