Click here to Skip to main content
15,899,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I am beginner.

Firstly, I'd like to start reading from this line as follow to end of the file:

1                                                      START OF RMS MAP


Than, I'd like to write specific line to text file.

I haven't succeeded in the first step.

Parent Directory: ftp://cddis.gsfc.nasa.gov/gnss/products/ionex/2017/

All files begin with codg.

I have 365 files beginning with codg.

What I have tried:

Regular Expression for this line: (s, @"^\s*1\s+START OF RMS MAP\S*$")

using System;
using System.IO;

namespace ReadFromFile
{
    class Program
    {
        private void ReadIt(StreamReader sr, StreamWriter sw)
        { 
            string line = sr.ReadLine();
            int blank, lat; 
            String select = ""; 

            while (line != null) 
            {
                blank = line.Split(' ').Length;
                if (line.Length == 80)  
                {
                    if (line.Substring(69, 3) == "RMS") 
                    if (line.Substring(60, 20) == "EPOCH OF CURRENT MAP") 
                        select = line.Substring(0, 24); 
                }
                if (blank > 6)
                {
                    try
                    {
                        if (line.Split(' ')[blank - 1] == "LAT/LON1/LON2/DLON/H") 
                        {
                            lat = Convert.ToInt32(line.Split(' ')[4].Substring(0, 2)); 
                            if ((lat == 30)) 
                            {
                                line = sr.ReadLine(); 
                                line = sr.ReadLine(); 
                                sw.WriteLine(select + " " + line.Substring(56, line.Length - 57)); 
                            }
                        }
                        line = sr.ReadLine();
                    }
                    catch
                    {
                        line = sr.ReadLine();
                    }
                }

            }
        }
        static void Main(string[] args)
        {
            string[] dir = Directory.GetFiles(@"C:\Users\Suat\Desktop\ionex\2017");
            int i = 0;
            Program P = new Program();
            StreamWriter sw = File.CreateText("C:/Users/Suat/Desktop/result.txt");
            for (i = 0; i < dir.Length; i++)
            {
                Console.Clear();
                Console.WriteLine(i.ToString() + "/" + dir.Length.ToString());
                StreamReader sr = File.OpenText(dir[i]);
                P.ReadIt(sr, sw);
                sr.Close();
            }
            sw.Close();
        }
    }
}
Posted
Updated 12-Aug-18 22:04pm
v4
Comments
Alexander Dymshyts 13-Aug-18 3:01am    
Please add some code, that you have tried
[no name] 13-Aug-18 4:01am    
This code not only gets data between "START OF RMS MAP" and "END OF RMS MAP", this code also gets "START OF TEC MAP" and "END OF TEC MAP". I just need to write data among "START OF RMS MAP" and "END OF RMS MAP" to text file.

using System;
using System.IO;

namespace ReadFromFile
{
    class Program
    {
        private void ReadIt(StreamReader sr, StreamWriter sw)
        { 
            string line = sr.ReadLine();
            int blank, lat; 
            String select = ""; 

            while (line != null) 
            {
                blank = line.Split(' ').Length;
                if (line.Length == 80)  
                {
                    if (line.Substring(69, 3) == "RMS") 
                    if (line.Substring(60, 20) == "EPOCH OF CURRENT MAP") 
                        select = line.Substring(0, 24); 
                }
                if (blank > 6)
                {
                    try
                    {
                        if (line.Split(' ')[blank - 1] == "LAT/LON1/LON2/DLON/H") 
                        {
                            lat = Convert.ToInt32(line.Split(' ')[4].Substring(0, 2)); 
                            if ((lat == 30)) 
                            {
                                line = sr.ReadLine(); 
                                line = sr.ReadLine(); 
                                sw.WriteLine(select + " " + line.Substring(56, line.Length - 57)); 
                            }
                        }
                        line = sr.ReadLine();
                    }
                    catch
                    {
                        line = sr.ReadLine();
                    }
                }

            }
        }
        static void Main(string[] args)
        {
            string[] dir = Directory.GetFiles(@"C:\Users\Suat\Desktop\ionex\2017");
            int i = 0;
            Program P = new Program();
            StreamWriter sw = File.CreateText("C:/Users/Suat/Desktop/result.txt");
            for (i = 0; i < dir.Length; i++)
            {
                Console.Clear();
                Console.WriteLine(i.ToString() + "/" + dir.Length.ToString());
                StreamReader sr = File.OpenText(dir[i]);
                P.ReadIt(sr, sw);
                sr.Close();
            }
            sw.Close();
        }
    }
}

1 solution

- Set output file to null.
- Open the input file.
- Read the next line.
- If content matches the search string then open the output file.
- If output file is not null write the input line to the output file.
- Repeat until end of input.
 
Share this answer
 

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