title: Extract Field Names and max lengths from a text file using C#
body: <p>I have a file that is a SQL Server result set saved as a text file. </p>
  
  <p>Here is a sample of what the file looks like:</p>
  
  <pre><code>RWS_DMP_ID      RV1_DMP_NUM      CUS_NAME
  3192            3957             THE ACME COMPANY                          
  3192            3957             THE ACME COMPANY                          
  3192            3957             THE ACME COMPANY   
  </code></pre>
  
  <p>I want to create a C# program that reads this file and creates the following table of data:</p>
  
  <pre><code>     Field       MaxSize
       -----       -------
   RWS_DMP_ID  17 
  RV1_DMP_NUM 17 
  CUS_NAME    42
  </code></pre>
  
  <p>This is a list of the field names and their max length. The max length is the beginning of the field to the space right before the beginning of the next field. </p>
  
  <p>By the way I don't care about code performance. This is seldom used file processing utility. </p>
  
  <p>I solved this with the following code:</p>
  
  <pre><code>objFile = new StreamReader(strPath + strFileName);
              strLine = objFile.ReadLine();
              intLineCnt = 0;
              while (strLine != null)
              {
                  intLineCnt++;
  
                  if (intLineCnt &lt;= 3)
                  {                       
                      if (intLineCnt == 1)
                      {
                          strWords = SplitWords(strLine);
                          intNumberOfFields = strWords.Length;
                          foreach (char c in strLine)
                          {
                              if (bolNewField == true)
                              {
                                  bolFieldEnd = false;
                                  bolNewField = false;
                              }
                              if (bolFieldEnd == false)
                              {
                                  if (c == ' ')
                                  {
                                      bolFieldEnd = true;
                                  }
                              }
                              else
                              {
                                  if (c != ' ')
                                  {
                                      if (intFieldCnt &lt; strWords.Length)
                                      {
                                          strProcessedData[intFieldCnt, 0] = strWords[intFieldCnt];
                                          strProcessedData[intFieldCnt, 1] = (intCharCnt - 1).ToString();
                                      }
                                      intFieldCnt++;
                                      intCharCnt = 1;
                                      bolNewField = true;
                                  }
                              }
                              if (bolNewField == false)
                              {
                                  intCharCnt++;
                              }
                          }
                          strProcessedData[intFieldCnt, 0] = strWords[intFieldCnt];
                          strProcessedData[intFieldCnt, 1] = intCharCnt.ToString();                               
                      }
                      else if (intLineCnt == 3)
                      {
                          intLine2Cnt= 0;
                          intTotalLength = 0;
                          while(intLine2Cnt &lt; intNumberOfFields)
                          {
                              intSize = Convert.ToInt32(strProcessedData[intLine2Cnt, 1]);
                              if (intSize + intTotalLength &gt; strLine.Length)
                              {
                                  intSize = strLine.Length - intTotalLength;
                              }
                              strField = strLine.Substring(intTotalLength, intSize);
                              strField = strField.Trim();
                              strProcessedData[intLine2Cnt, intLineCnt - 1] = strField;
                              intTotalLength = intTotalLength + intSize + 1;                                
  
                              intLine2Cnt++;
                          }
                      }                       
                  }
                  strLine = objFile.ReadLine();
              }`enter code here`
  </code></pre>
  
  <p>I'm aware that this code is a complete hack job. I'm looking for a better way to solve this problem. </p>
  
  <p>Is there a better way to solve this problem? </p>
  
  <p>THanks</p>