Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. a better way?

a better way?

Scheduled Pinned Locked Moved C#
csharpdatabaseperformancehelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 508833
    wrote on last edited by
    #1

    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.

    B 1 Reply Last reply
    0
    • U User 508833

      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.

      B Offline
      B Offline
      Burt Harris
      wrote on last edited by
      #2

      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

      U 1 Reply Last reply
      0
      • B 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 Harris

        U Offline
        U Offline
        User 508833
        wrote on last edited by
        #3

        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

        B 1 Reply Last reply
        0
        • U User 508833

          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

          B Offline
          B Offline
          Burt Harris
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups