I am just learning to code in C# and need to parse a file that is in the following format:
[VOR]
YMS 114.500 N044.08.35.400 W080.08.46.200
YTP 116.550 N043.40.18.00 W079.39.51.000
YWT 115.000 N043.27.31.200 W080.22.45.600
YSO 117.350 N044.14.19.200 W079.10.30.000
YYZ 112.150 N043.39.28.800 W079.37.54.000
[NDB]
OO 391 N043.55.18.000 W078.54.18.000
KZ 248 N043.56.01.000 W079.19.44.999
TZ 257 N043.36.46.000 W079.23.07.999
ZDH 385 N043.44.17.000 W079.34.11.000
ZLP 341 N043.37.41.000 W079.43.55.000
ZTO 403.000 N043.44.19.671 W079.42.09.868
ZYZ 368.000 N043.37.10.222 W079.32.56.396
[Next Section]
....
The amount of data in each section is different in every file.
I need a way to get data from one section at a time. I know how to read and split the lines but cant seem to figure out how to only grab data from one section.
Any help would be great.
Thanks
7 1663
search for the line which contains ' [ ' and ' ] ' then start a new reading process
Or search for the blank line between the blocks
So I have decided to parse the entire file using 2 passes.
The first pass it to get the number of data fields in each section and the second pass will actually populate the data into their respective arrays.
here is the code for the first pass:
By defining all your variables within the method none of them available one the method completes and returns.
You aren't doing anything within the method with the counts.
So what happens next? It is almost as if all this processing gets thrown away.
as of right now the method does nothing but count the amount of data in each section.
i am currently writing the second pass of the file where i will setup array for all the data. I am simple using all the count values to define the array size. The arrays will return the data to the main form.
string[] strInfo = new string[infocount];
i will post the second pass once i have it written
:D
Here is the final class. It calculates how many data fields in each section. (completed in the first pass). Then puts the data into the appropriate public string arrays.(completed in the second pass) -
class SectorFileParser
-
{
-
//Declare Sector File Data Arrays
-
public string[] strColorName,strColorValue,strInfo;
-
public string[] strAirportID, strAirportFreq, strAirportLat, strAirportLon, strAirportClass;
-
public string[] strRunwayID, strRunwayHeadings, strRunwayStartLat, strRunwayStartLon, strRunwayEndLat, strRunwayEndLon;
-
public string[] strVorID, strVorFreq, strVorLat, strVorLon;
-
public string[] strNDBID, strNDBFreq, strNDBLat, strNDBLon;
-
public string[] strFixID, strFixLat, strFixLon;
-
public string[] strLabelID, strLabelLat, strLabelLon, strLabelColor;
-
public string[] strLowAirwayID, strLowAirwayStartLat, strLowAirwayStartLon, strLowAirwayEndLat, strLowAirwayEndLon;
-
public string[] strHighAirwayID, strHighAirwayStartLat, strHighAirwayStartLon, strHighAirwayEndLat, strHighAirwayEndLon;
-
public string[] strGeoStartLat, strGeoStartLon, strGeoEndLat, strGeoEndLon, strGeoColor;
-
public string[] strArtccLowID, strArtccLowStartLat, strArtccLowStartLon, strArtccLowEndLat, strArtccLowEndLon;
-
public string[] strArtccHighID, strArtccHighStartLat, strArtccHighStartLon, strArtccHighEndLat, strArtccHighEndLon;
-
public string[] strStarID, strStarStartLat, strStarStartLon, strStarEndLat, strStarEndLon ,strStarColor ;
-
public string[] strSIDID, strSIDStartLat, strSIDStartLon, strSIDEndLat, strSIDEndLon, strSIDColor;
-
-
public void OpenSector(string strFile)
-
{
-
if (File.Exists(strFile))
-
{
-
//Start Pass 1
-
using (StreamReader re = File.OpenText(strFile))
-
{
-
int lines=0;//For Line Counting
-
int comments = 0;//Counts Comment Lines
-
int blanks = 0;//Counts Blank Lines
-
int sectioncount = 0;//Counts Sections In Sector "[...]"
-
int colorcount = 0;//Counts Color Definitions #define AQUA 16776960
-
int infocount = 0;//Counts Info Lines
-
int airportcount = 0;//Counts Airports
-
int runwaycount = 0;//Counts Runsways
-
int vorcount = 0;//Counts VORs
-
int ndbcount = 0;//Counts NDBs
-
int fixcount = 0;//Counts Fixes
-
int labelcount = 0;//Counts Labels
-
int lowairwaycount = 0;//Counts Low Airways
-
int highairwaycount = 0;//Counts High Airways
-
int geocount = 0;//Counts Geo Data
-
int artcclow = 0;//Counts artcc lower boundries
-
int artcchigh = 0;//Counts artcc high boundries
-
int starcount = 0;//Counts Stars
-
int sidcount = 0;//Counts Sids
-
-
String input;
-
string section = "";
-
-
while ((input=re.ReadLine())!=null)
-
{
-
lines = lines + 1;
-
if (input == "")
-
{
-
blanks = blanks + 1;
-
}
-
else if (input.Substring(0, 1) == ";")
-
{
-
//Is a comment
-
comments = comments + 1;
-
}
-
else if (input.Substring(0, 1) == "#")
-
{
-
//Is a comment
-
colorcount = colorcount + 1;
-
}
-
else if (input.Substring(0, 1) == "[")
-
{
-
sectioncount = sectioncount + 1;
-
section = input.Substring(0, input.IndexOf("]") + 1);
-
}
-
else
-
{
-
switch (section.ToUpper())
-
{
-
case "[INFO]":
-
infocount = infocount + 1;
-
break;
-
case "[AIRPORT]":
-
airportcount = airportcount + 1;
-
break;
-
case "[RUNWAY]":
-
runwaycount = runwaycount + 1;
-
break;
-
case "[VOR]":
-
vorcount = vorcount + 1;
-
break;
-
case "[NDB]":
-
ndbcount = ndbcount + 1;
-
break;
-
case "[FIXES]":
-
fixcount = fixcount + 1;
-
break;
-
case "[LABELS]":
-
labelcount = labelcount + 1;
-
break;
-
case "[LOW AIRWAY]":
-
lowairwaycount = lowairwaycount + 1;
-
break;
-
case "[HIGH AIRWAY]":
-
highairwaycount = highairwaycount + 1;
-
break;
-
case "[GEO]":
-
geocount = geocount + 1;
-
break;
-
case "[ARTCC LOW]":
-
//Identify Names Here To Get Accurate Count
-
artcclow = artcclow + 1;
-
break;
-
case "[ARTCC HIGH]":
-
//Identify Names Here To Get Accurate Count
-
artcchigh = artcchigh + 1;
-
break;
-
case "[STAR]":
-
//Identify Names Here To Get Accurate Count
-
starcount = starcount + 1;
-
break;
-
case "[SID]":
-
//Identify Names Here To Get Accurate Count
-
sidcount = sidcount + 1;
-
break;
-
}
-
}
-
}
-
-
//Pass 1 Complete
-
re.Close();//Close Stream Reader
-
-
-
//Start Pass 2
-
-
//Setup Color Arrays
-
int tempcolor = -1;
-
strColorName = new string[colorcount];
-
strColorValue = new string[colorcount];
-
-
//Setup Info Array
-
int tempinfo = -1;
-
strInfo = new string[infocount];
-
-
//Setup Runaway Arrays
-
int temprunways = -1;
-
strRunwayID = new string[runwaycount];
-
strRunwayHeadings = new string[runwaycount];
-
strRunwayStartLat = new string[runwaycount];
-
strRunwayStartLon = new string[runwaycount];
-
strRunwayEndLat = new string[runwaycount];
-
strRunwayEndLon = new string[runwaycount];
-
-
//Setup Airport Arrays
-
int tempairport = -1;
-
strAirportID = new string[airportcount];
-
strAirportFreq = new string[airportcount];
-
strAirportLat = new string[airportcount];
-
strAirportLon = new string[airportcount];
-
strAirportClass = new string[airportcount];
-
-
//Setup VOR Arrays
-
int tempVor = -1;
-
strVorID = new string[vorcount];
-
strVorFreq = new string[vorcount];
-
strVorLat = new string[vorcount];
-
strVorLon = new string[vorcount];
-
-
//Setup NDB Arrays
-
int tempNDB = -1;
-
strNDBID = new string[ndbcount];
-
strNDBFreq = new string[ndbcount];
-
strNDBLat = new string[ndbcount];
-
strNDBLon = new string[ndbcount];
-
-
//Setup Fix Arrays
-
int tempFix = -1;
-
strFixID = new string[fixcount];
-
strFixLat = new string[fixcount];
-
strFixLon = new string[fixcount];
-
-
//Setup Label Arrays
-
int tempLabel = -1;
-
strLabelID=new string[labelcount];
-
strLabelLat = new string[labelcount];
-
strLabelLon = new string[labelcount];
-
strLabelColor = new string[labelcount];
-
-
//Setup Low Airway Arrays
-
int tempLowAirways = -1;
-
strLowAirwayID = new string[lowairwaycount];
-
strLowAirwayStartLat = new string[lowairwaycount];
-
strLowAirwayStartLon = new string[lowairwaycount];
-
strLowAirwayEndLat = new string[lowairwaycount];
-
strLowAirwayEndLon = new string[lowairwaycount];
-
-
//Setup High Airway Arrays
-
int tempHighAirways = -1;
-
strHighAirwayID = new string[highairwaycount];
-
strHighAirwayStartLat = new string[highairwaycount];
-
strHighAirwayStartLon = new string[highairwaycount];
-
strHighAirwayEndLat = new string[highairwaycount];
-
strHighAirwayEndLon = new string[highairwaycount];
-
-
//Setup Geo Data Arrays
-
int tempGeo = -1;
-
strGeoStartLat = new string[geocount];
-
strGeoStartLon = new string[geocount];
-
strGeoEndLat = new string[geocount];
-
strGeoEndLon = new string[geocount];
-
strGeoColor = new string[geocount];
-
-
//Setup Low ARTCC Arrays
-
int tempARTCCLow = -1;
-
strArtccLowID = new string[artcclow];
-
strArtccLowStartLat = new string[artcclow];
-
strArtccLowStartLon = new string[artcclow];
-
strArtccLowEndLat = new string[artcclow];
-
strArtccLowEndLon = new string[artcclow];
-
-
//Setup High ARTCC Arrays
-
int tempARTCCHigh = -1;
-
strArtccHighID = new string[artcchigh];
-
strArtccHighStartLat = new string[artcchigh];
-
strArtccHighStartLon = new string[artcchigh];
-
strArtccHighEndLat = new string[artcchigh];
-
strArtccHighEndLon = new string[artcchigh];
-
-
//Setup Star Arrays
-
int tempStar = -1;
-
strStarID=new string[starcount];
-
strStarStartLat = new string[starcount];
-
strStarStartLon = new string[starcount];
-
strStarEndLat = new string[starcount];
-
strStarEndLon = new string[starcount];
-
strStarColor = new string[starcount];
-
-
//Setup Sid Arrays
-
int tempSid = -1;
-
strSIDID = new string[sidcount];
-
strSIDStartLat = new string[sidcount];
-
strSIDStartLon = new string[sidcount];
-
strSIDEndLat = new string[sidcount];
-
strSIDEndLon = new string[sidcount];
-
strSIDColor = new string[sidcount];
-
-
//Read The Data
-
using (StreamReader re2 = File.OpenText(strFile))
-
{
-
while ((input = re2.ReadLine()) != null)
-
{
-
-
if (input == "")
-
{
-
//Blank Line So Ignore It
-
}
-
else if (input.Substring(0, 1) == ";")
-
{
-
//Is a comment so Ignore it
-
-
}
-
else if (input.Substring(0, 1) == "#")
-
{
-
//Load Colors Into Array
-
tempcolor = tempcolor + 1;
-
string[] parsedcolor = input.Split(new Char [] {' '});
-
strColorName[tempcolor] = parsedcolor[1];
-
strColorValue[tempcolor] = parsedcolor[2];
-
}
-
else if (input.Substring(0, 1) == "[")
-
{
-
sectioncount = sectioncount + 1;
-
section = input.Substring(0, input.IndexOf("]") + 1);
-
}
-
else
-
{
-
//Steralize Comments Off Ends Of Data
-
if (input.IndexOf(";") > 0)
-
{
-
input = input.Substring(0, input.IndexOf(";" ));
-
}
-
switch (section.ToUpper())
-
{
-
case "[INFO]":
-
tempinfo = tempinfo + 1;
-
strInfo[tempinfo] = input;
-
break;
-
case "[AIRPORT]":
-
tempairport = tempairport + 1;
-
string[] parsedairport = input.Split(new Char[] { ' ' });
-
strAirportID[tempairport] = parsedairport[0];
-
strAirportFreq[tempairport] = parsedairport[1];
-
strAirportLat[tempairport] = parsedairport[2];
-
strAirportLon[tempairport] = parsedairport[3];
-
strAirportClass[tempairport] = parsedairport[4];
-
break;
-
case "[RUNWAY]":
-
temprunways = temprunways + 1;
-
string[] parsedrunways = input.Split(new Char[] { ' ' });
-
strRunwayID[temprunways] = parsedrunways[0] + "/" + parsedrunways[1];
-
strRunwayHeadings[temprunways] = parsedrunways[2] + "/" + parsedrunways[3];
-
strRunwayStartLat[temprunways] = parsedrunways[4];
-
strRunwayStartLon[temprunways] = parsedrunways[5];
-
strRunwayEndLat[temprunways] = parsedrunways[6];
-
strRunwayEndLon[temprunways] = parsedrunways[7];
-
break;
-
case "[VOR]":
-
tempVor = tempVor + 1;
-
string[] parsedVORs = input.Split(new Char[] { ' ' });
-
strVorID[tempVor] = parsedVORs[0];
-
strVorFreq[tempVor] = parsedVORs[1];
-
strVorLat[tempVor] = parsedVORs[2];
-
strVorLon[tempVor] = parsedVORs[3];
-
break;
-
case "[NDB]":
-
tempNDB = tempNDB + 1;
-
string[] parsedNDBs = input.Split(new Char[] { ' ' });
-
strNDBID[tempNDB] = parsedNDBs[0];
-
strNDBFreq[tempNDB] = parsedNDBs[1];
-
strNDBLat[tempNDB] = parsedNDBs[2];
-
strNDBLon[tempNDB] = parsedNDBs[3];
-
break;
-
case "[FIXES]":
-
tempFix = tempFix + 1;
-
string[] parsedfix=input.Split(new Char[] { ' ' });
-
strFixID[tempFix] = parsedfix[0];
-
strFixLat[tempFix] = parsedfix[1];
-
strFixLon[tempFix] = parsedfix[2];
-
break;
-
case "[LABELS]":
-
tempLabel = tempLabel + 1;
-
string[] parsedLabel = input.Split(new Char[] { ' ' });
-
strLabelID[tempLabel] = parsedLabel[0].Substring(1, parsedLabel[0].Length - 2);
-
strLabelLat[tempLabel] = parsedLabel[1];
-
strLabelLon[tempLabel] = parsedLabel[2];
-
strLabelColor[tempLabel] = parsedLabel[3];
-
break;
-
case "[LOW AIRWAY]":
-
tempLowAirways = tempLowAirways + 1;
-
string[] parsedLowAirways= input.Split(new Char[] { ' ' });
-
strLowAirwayID[tempLowAirways] = parsedLowAirways[0];
-
strLowAirwayStartLat[tempLowAirways] = parsedLowAirways[1];
-
strLowAirwayStartLon[tempLowAirways] = parsedLowAirways[2];
-
strLowAirwayEndLat[tempLowAirways] = parsedLowAirways[3];
-
strLowAirwayEndLon[tempLowAirways] = parsedLowAirways[4];
-
break;
-
case "[HIGH AIRWAY]":
-
tempHighAirways = tempHighAirways + 1;
-
string[] parsedHighAirways= input.Split(new Char[] { ' ' });
-
strHighAirwayID[tempHighAirways] = parsedHighAirways[0];
-
strHighAirwayStartLat[tempHighAirways] = parsedHighAirways[1];
-
strHighAirwayStartLon[tempHighAirways] = parsedHighAirways[2];
-
strHighAirwayEndLat[tempHighAirways] = parsedHighAirways[3];
-
strHighAirwayEndLon[tempHighAirways] = parsedHighAirways[4];
-
break;
-
case "[GEO]":
-
tempGeo = tempGeo + 1;
-
string[] parsedGeo = input.Split(new Char[] { ' ' });
-
strGeoStartLat[tempGeo] = parsedGeo[0];
-
strGeoStartLon[tempGeo] = parsedGeo[1];
-
strGeoEndLat[tempGeo] = parsedGeo[2];
-
strGeoEndLon[tempGeo] = parsedGeo[3];
-
strGeoColor[tempGeo] = parsedGeo[4];
-
break;
-
case "[ARTCC LOW]":
-
tempARTCCLow = tempARTCCLow + 1;
-
string[] parsedARTCCLow = input.Split(new Char[] { ' ' });
-
strArtccLowID[tempARTCCLow] = parsedARTCCLow[0];
-
strArtccLowStartLat[tempARTCCLow] = parsedARTCCLow[1];
-
strArtccLowStartLon[tempARTCCLow] = parsedARTCCLow[2];
-
strArtccLowEndLat[tempARTCCLow] = parsedARTCCLow[3];
-
strArtccLowEndLon[tempARTCCLow] = parsedARTCCLow[4];
-
break;
-
case "[ARTCC HIGH]":
-
tempARTCCHigh = tempARTCCHigh + 1;
-
string[] parsedARTCCHigh = input.Split(new Char[] { ' ' });
-
strArtccHighID[tempARTCCHigh] = parsedARTCCHigh[0];
-
strArtccHighStartLat[tempARTCCHigh] = parsedARTCCHigh[1];
-
strArtccHighStartLon[tempARTCCHigh] = parsedARTCCHigh[2];
-
strArtccHighEndLat[tempARTCCHigh] = parsedARTCCHigh[3];
-
strArtccHighEndLon[tempARTCCHigh] = parsedARTCCHigh[4];
-
break;
-
case "[STAR]":
-
tempStar = tempStar + 1;
-
strStarID[tempStar] = input.Substring(0, 26);
-
string[] parsedStars = input.Substring(26).Split(new Char[] { ' ' });
-
strStarStartLat[tempStar] = parsedStars[0];
-
strStarStartLon[tempStar] = parsedStars[1];
-
strStarEndLat[tempStar] = parsedStars[2];
-
strStarEndLon[tempStar] = parsedStars[3];
-
try
-
{
-
strStarColor[tempStar] = parsedStars[4];
-
}
-
catch //If No Color Provided....
-
{
-
strStarColor[tempStar] = "";
-
}
-
break;
-
case "[SID]":
-
tempSid = tempSid + 1;
-
strSIDID[tempSid] = input.Substring(0, 26);
-
string[] parsedSIDs = input.Substring(26).Split(new Char[] { ' ' });
-
strSIDStartLat[tempSid] = parsedSIDs[0];
-
strSIDStartLon[tempSid] = parsedSIDs[1];
-
strSIDEndLat[tempSid] = parsedSIDs[2];
-
strSIDEndLon[tempSid] = parsedSIDs[3];
-
try
-
{
-
strSIDColor[tempSid] = parsedSIDs[4];
-
}
-
catch //If No Color Provided....
-
{
-
strSIDColor[tempSid] = "";
-
}
-
break;
-
}
-
}
-
}
-
-
//Reading Done
-
re2.Close();//Close Stream Reader
-
}
-
-
-
-
-
}
-
}
-
else
-
{
-
//File Not Found
-
}
-
-
}
-
-
Here's a few tips to help you compact your code down...
1) You can use the generic List class instead of string arrays so you don't need to do the count. Define a list and add to it as such... - List<string> myList = new List<string>();
-
myList.Add("a string!");
Other than that, they work just like arrays.
2) Use hash tables to eliminate your switch statement... a hash table has a key and an object. For example, you could do the following... - Hashtable sectionHash = new Hashtable();
-
string section = ... (however you get it here);
-
-
if (!sectionHash.ContainsKey(section))
-
sectionHash[section] = new List<string>();
-
-
sectionHash[section].Add(... whatever ...);
Since you've got quite a bit of stuff to add here, you could even put it all in a struct so you don't need to keep so many arrays.
---------------------
Just some ideas, hopefully they're of use to you.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: chuck amadi |
last post by:
Hi , Im trying to parse a specific users mailbox (testwwws) and output
the body of the messages to a file ,that file will then be loaded into a
PostGresql DB at some point .
I have read the...
|
by: chuck amadi |
last post by:
By the way list is there a better way than using the readlines() to
> > >parse the mail data into a file , because Im using
> > >email.message_from_file it returns
> > >all the data i.e reads one...
|
by: nate |
last post by:
Hello,
Does anyone know where I can find an ASP server side script written in
JavaScript to parse text fields from a form method='POST' using
enctype='multipart/form-data'? I'd also like it to...
|
by: Johnny Google |
last post by:
Here is an example of the type of data from a file I will have:
Apple,4322,3435,4653,6543,4652
Banana,6934,5423,6753,6531
Carrot,3454,4534,3434,1111,9120,5453
Cheese,4411,5522,6622,6641
The...
|
by: DH |
last post by:
Hi,
I'm trying to strip the html and other useless junk from a html page..
Id like to create something like an automated text editor, where it
takes the keywords from a txt file and removes them...
|
by: trevor |
last post by:
Incorrect values when using float.Parse(string)
I have discovered a problem with float.Parse(string) not getting values
exactly correct in some circumstances(CSV file source) but in very
similar...
|
by: AdrianH |
last post by:
Assumptions
I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming.
FYI
Although I have called...
|
by: AdrianH |
last post by:
Assumptions
I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming.
FYI
Although I have called this...
|
by: =?Utf-8?B?RGF2aWRN?= |
last post by:
Hello, I have an XML file generated from a third party application that I
would like to parse. Ideally, I plan on having a windows service setup to
scan various folders for XML files and parse the...
|
by: goldtech |
last post by:
SAX XML Parse Python error message
Hi,
My first attempt at SAX, but have an error message I need help with.
I cite the error message, code, and xml below.
Be grateful if anyone can tell me...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |