How to read a part of a text file and put them in an array in c#
0 #1 1 Day Ago
I have a text file including several strings and doubles. I want to find a specific string in the text and read all double numbers after it which are located between two slashes"/". The number of double data could be unknown-but we just know they are bounded with two slashes- the file is two big and including several strings for example my data file is like this and I want to put all Double numbers,bounded between slashes and after "POROSITY" into an array with name "POROSITY".
*****************************************************
Permeability
/
1 2 3 4
5 6 7 8
9 10
11 12 13 14
15 15 17 18
19 20
21 22 23 24
25 26 27 28
29 30
.
.
.
/
POROSITY
/
1 2 3 4
5 6 7 8
9 10
11 12 13 14
15 15 17 18
19 20
21 22 23 24
25 26 27 28
29 30
.
.
.
/

Read file, when you read POROSITY read "/"
Then read all following chars until "/" into a string array.
Don't know with what chars your doubles are separated, but if you know, use the string.Split method to separate all doubles in an array.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmDoubleRead : Form
  {
    public frmDoubleRead()
    {
      InitializeComponent();
    }

    private static string[] GetTestData()
    {
      List<string> result = new List<string>();
      result.Add("Permeability");
      result.Add("/");
      result.Add("1 2 3 4");
      result.Add("5 6 7 8");
      result.Add("9 10");
      result.Add("11 12 13 14");
      result.Add("15 15 17 18");
      result.Add("19 20");
      result.Add("21 22 23 24");
      result.Add("25 26 27 28");
      result.Add("29 30");
      result.Add(".");
      result.Add(".");
      result.Add(".");
      result.Add("/");
      result.Add("POROSITY");
      result.Add("/");
      result.Add("1 2 3 4");
      result.Add("5 6 7 8");
      result.Add("9 10");
      result.Add("11 12 13 14");
      result.Add("15 15 17 18");
      result.Add("19 20");
      result.Add("21 22 23 24");
      result.Add("25 26 27 28");
      result.Add("29 30");
      result.Add(".");
      result.Add(".");
      result.Add(".");
      result.Add("/ ");
      return result.ToArray();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Dictionary<string, List<int>> dict = new Dictionary<string, List<int>>(StringComparer.CurrentCultureIgnoreCase);

      const string TABLE_BEGIN = @"/";
      //string[] lines = System.IO.File.ReadAllLines(@"C:\data.txt");
      string[] lines = GetTestData();

      string curTable = string.Empty;
      List<int> curWorkingSet = null;

      for (int i1 = 0; i1 < lines.Length; i1++)
      {
        string trimmedLine = (string.IsNullOrEmpty(lines[i1]) ? string.Empty : lines[i1].Trim());
        if (trimmedLine.Equals(TABLE_BEGIN))
        {
          if (string.IsNullOrEmpty(curTable))
          {
            curTable = lines[i1 - 1];
            if (!dict.TryGetValue(curTable, out curWorkingSet))
            {
              curWorkingSet = new List<int>();
              dict.Add(curTable, curWorkingSet);
            }
          }
          else
            curTable = string.Empty;
        }
        string[] lineFields = trimmedLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string s in lineFields)
        {
          int i;
          if (int.TryParse(s, out i))
            curWorkingSet.Add(i);
        }
      }

      foreach (KeyValuePair<string, List<int>> kvp in dict)
      {
        string key = kvp.Key;
        int[] iValues = kvp.Value.ToArray();
        string[] sValues = Array.ConvertAll<int, string>(iValues, Convert.ToString);
        Console.WriteLine("{0}: {1}",
          key,
          string.Join(", ", sValues));
      }
    }

  }
}

Results in:

Permeability: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
POROSITY: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
Comments
i would give my left nut to have your code library! :p
This article has been dead for over six months. Start a new discussion instead.