a better way?
-
i'm trying to use the streamreader in csharp to read a .ged file (ascii text). i got it to work but i want to know if there is anything i can do to make it better, ie. programming format, speed, style, etc. thanks, Rob <-----------START-----------> public string[] GetIndex() { // if (fstream == null) { MessageBox.Show("Error opening file stream.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); return null; } StreamReader fstreamCopy = fstream; string textLine; int index; bool foundName = false; ArrayList tmpIndex = new ArrayList(); while (fstreamCopy.BaseStream != null) { foundName = false; try { textLine = fstreamCopy.ReadLine().Trim(); index = textLine.IndexOf("INDI"); if (index != -1) { while (!foundName) { textLine = fstreamCopy.ReadLine().Trim(); index = textLine.IndexOf("NAME"); if (index != -1) { tmpIndex.Add(textLine.Substring(index + 4).Trim()); foundName = true; } } } } catch { fstreamCopy.Close(); } } string[] indIndex = new String[tmpIndex.Count]; for (int i = 0; i < tmpIndex.Count; i++) { indIndex[i] = tmpIndex[i].ToString(); } return (indIndex); // } <------------END------------> There are 10 kinds of people. Those who understand binary and those who don't.
-
i'm trying to use the streamreader in csharp to read a .ged file (ascii text). i got it to work but i want to know if there is anything i can do to make it better, ie. programming format, speed, style, etc. thanks, Rob <-----------START-----------> public string[] GetIndex() { // if (fstream == null) { MessageBox.Show("Error opening file stream.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); return null; } StreamReader fstreamCopy = fstream; string textLine; int index; bool foundName = false; ArrayList tmpIndex = new ArrayList(); while (fstreamCopy.BaseStream != null) { foundName = false; try { textLine = fstreamCopy.ReadLine().Trim(); index = textLine.IndexOf("INDI"); if (index != -1) { while (!foundName) { textLine = fstreamCopy.ReadLine().Trim(); index = textLine.IndexOf("NAME"); if (index != -1) { tmpIndex.Add(textLine.Substring(index + 4).Trim()); foundName = true; } } } } catch { fstreamCopy.Close(); } } string[] indIndex = new String[tmpIndex.Count]; for (int i = 0; i < tmpIndex.Count; i++) { indIndex[i] = tmpIndex[i].ToString(); } return (indIndex); // } <------------END------------> There are 10 kinds of people. Those who understand binary and those who don't.
Great, I'll try to add a few constructive comments: 1) Early on, you handle a null value for fstream by showing a message box, then returning a null from the GetIndex() function. Typically, in C# we handle that sort of unexpected condition by throwing a an exception, rather than returning null. Error handling can be done at higher levels with try/catch blocks. 2) I'm guessing you might be thinking the line
StreamReader fstreamCopy = fstream;
is copying the original StreamReader fstream, so that it's position won't be changed. It doesn't work that way, since StreamReader is a reference class, you assignment just creates an alternate name for the same object fstream referrs to. 3) At the bottom, you loop through the ArrayList to create a string array to return. There might be a better way to do this:tmpIndex.CopyTo( 0, indIndex, 0, tmpIndex.Count );
Burt Harris -
Great, I'll try to add a few constructive comments: 1) Early on, you handle a null value for fstream by showing a message box, then returning a null from the GetIndex() function. Typically, in C# we handle that sort of unexpected condition by throwing a an exception, rather than returning null. Error handling can be done at higher levels with try/catch blocks. 2) I'm guessing you might be thinking the line
StreamReader fstreamCopy = fstream;
is copying the original StreamReader fstream, so that it's position won't be changed. It doesn't work that way, since StreamReader is a reference class, you assignment just creates an alternate name for the same object fstream referrs to. 3) At the bottom, you loop through the ArrayList to create a string array to return. There might be a better way to do this:tmpIndex.CopyTo( 0, indIndex, 0, tmpIndex.Count );
Burt Harristhanks for the help and information. what would you recommend to be the best way to read an ascii file? can i keep it in memory somehow so i can search it at will or should i just read the file everytime i need to? thanks, Rob
-
thanks for the help and information. what would you recommend to be the best way to read an ascii file? can i keep it in memory somehow so i can search it at will or should i just read the file everytime i need to? thanks, Rob
You're welcome. Sorry for the delay, I've been on vacation, RVing the Oregon Coast. It was really great. I'm not sure there is any generic "best" way, it depends on what you want to do with it. Keeping it in memory may help performance slightly, but the OS does enough caching that it's likely to have only a minimal effect in most cases, so I'd concentrate on what's most convienent from your application's standpoint to start. In many cases, I think about making a pass through a file, building an in-memory data structure from it's contents because I want to access it randomly (say by a persons's name) rather than sequentially. Good luck Burt Harris