Skip to content

Text Files -- Extracting Data

Jerry Fike

Asked Jan 7, 2010, 9:26 AM EST1 answer
Share
LinkedInX
I am working on some code to extract some blocks of text data from rather large files.  In one file, there are 13 blocks of text ranging from around 30,000 to 150,000 bytes.  I was using the simple code below to extract and record the data into one block, but it seems extremely slow (several minutes.)  Since it reads into filebuffer from disk in a couple of seconds, it was surprising to me it takes so long to transfer out the blocks of data, especially considering the start and end positions are already identified.

Is there a better way to do this to gain some speed?

Code
for (int i = 0; i < tgoBlockCount; i++)
            {
                for (int j = tgoBlockStart[i]; j < tgoBlockEnd[i]; j++)
                {
                    tgoBlock += fileBuffer[j];
                }
            }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I figured out a much faster way, figuring that the tgoBlock += fileBuffer[j]; was the problem. The code below take less than a second now.

    char[] tgoChar= new char[tgoSize];
    int k = 0;

    for (int i = 0; i < tgoBlockCount; i++)
    {
      for (int j = tgoBlockStart[i]; j < tgoBlockEnd[i]; j++)
      {
        tgoChar[k] = fileBuffer[j];
        k++;
      }
    }
    string tgoString = new string(tgoChar);
    tgoBlock = tgoString;
    0