Click here to Skip to main content
15,913,027 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i want to extract data from a text file

I have a text file in the following format-
VB
2002/01/10 00:44:51.53 40.4415 -126.0167 25.37 3.92 Md 56 269 147 0.29 NCSN 21208454


i have used the following code to get the data from the text file
C#
public class data
{
    public string date, time;
    public double lat, lon,depth,mag;
}

class Program
{

    static void Main(string[] args)
    {
        string dt;
        List<data> gd = new List<data>();
        using (StreamReader sr = new StreamReader("E:\\op.html"))
        {
            while (sr.Peek() > 0)
            {
                string str;
                string[] arr;
                str = sr.ReadLine();
                arr = str.Split(' ');
                data d = new data();
                d.date = arr[0];
                //d.time = arr[1];
                //d.lat = Convert.ToDouble( arr[2]);
                //d.lon = Convert.ToDouble(arr[3]);
                //d.depth = Convert.ToDouble(arr[4]);
                //d.mag = Convert.ToDouble(arr[5]);


                Console.WriteLine(d.date);
                //Console.WriteLine(d.time);
                //Console.WriteLine(d.lat);
                //Console.WriteLine(d.lon);
                //Console.WriteLine(d.depth);
                //Console.WriteLine(d.mag);
                Console.ReadKey();

            }



in the above code i get the value only of d.date, but the lines marked with "//" throws an error "Index out of bounds..." & i cant get the rest of the values.
i want the values for d.time,d.lat,d.lon,d.depth,d.mag..
how do i achieve this ??
Posted
Updated 26-Jul-13 7:39am
v2
Comments
Matt T Heffron 26-Jul-13 13:44pm    
Are you sure that is the actual format of the file?
I'm asking because your code shows reading from an HTML file.
(Looks like Earthquake data...;-) )
Member 8657306 26-Jul-13 13:50pm    
yeah it is the actual content of the file (both html & text look the same)
Matt T Heffron 26-Jul-13 13:58pm    
The code you've given works perfectly even when I uncomment the other arr references.
Sergey Alexandrovich Kryukov 26-Jul-13 13:48pm    
Why would you re-post the same exact question when I gave you a very detailed answer. Did you have a chance to read my solution?
—SA
Member 8657306 26-Jul-13 13:49pm    
dint get a chance actually

1 solution

Well, the idea is basically correct. Of course, such parsing is rigid and fragile, as a little change in format would break the functionality. Of course, it can be improved, but only slightly.

The fix you need is this:

C#
using (StreamReader sr = new StreamReader("TestFile.txt")) {
   string line;
   while ((line = sr.ReadLine()) != null) {
       // remove all lines from your previous code inside loop before Split
       string arr[] = line.Split(new char[] {' '}, System.StringSplitOptions.RemoveEmptyEntries);
       // ...
   } //loop
} // using; here is where sr.Dispose is automatically called


Pay attention: System.StringSplitOptions.RemoveEmptyEntries will add some stability, in case you have more than one blank space or other complication (you can combine several separators making a first parameter a character array).

And you don't other fields because… 5 next lines are commented out. :-)
Add them again, as far as I can see, they should work. Run it all under debugger. Even the minimal debugging skills will help you to get it right in no time.

Good luck,
—SA
 
Share this answer
 
v2
Comments
Member 8657306 26-Jul-13 14:17pm    
thanx gt it :)
Sergey Alexandrovich Kryukov 26-Jul-13 15:30pm    
Great. You are welcome.
Good luck, call again.
—SA
Sergey Alexandrovich Kryukov 26-Jul-13 15:32pm    
And I modified the Split parameter in question to make it compile, thank you for the note, sorry for the inconvenience.
—SA
Member 8657306 29-Jul-13 6:27am    
hey i have encountered an problem, the arr[8] & arr[6] skips out certain values from the text file.
i.e. in certain cases,it returns empty string even though data exists in the text file.

how do i sort it out?
Sergey Alexandrovich Kryukov 29-Jul-13 10:14am    
Way too simple: debug those cases, running it under the debugger.
—SA

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