一、問題描述

如何使用C#語言來讀取 .txt檔案,方便進一步的程式運算。

二、方法

第一種方式是將陣列變成array[x, y]的型式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Noise_Removal_Csharp
{
     class Program_Main
     {
          static void Main(string[] args)
          {

            // ****** Step 1: Read Data ******
             const string baseline_filename = "baseline_eeg_data.txt";

             int channel_num = 32;
             string baseline_file = System.IO.File.ReadAllText(baseline_filename);
             char[] separators = { ' ', '\n', '\r', '\t', ',', ',', '!' };
             string[] tempArray_baseline = baseline_file.Split(separators, StringSplitOptions.RemoveEmptyEntries);
             int count_baseline_array_num = 0;
             foreach (string tmp in tempArray_baseline)
             {
                  count_baseline_array_num ++;
             }
             double[,] baseline_eeg = new double[channel_num, count_baseline_array_num / channel_num];
             int i,j,k = 0;
             for (i = 0; i < (channel_num); i++)
             {
                  for(j = 0; j < (count_baseline_array_num / channel_num); j++)
                  {
                        baseline_eeg[i, j] = double.Parse(tempArray_baseline[k]);
                        k++;
                  }
             }
          }
     }
}

====================================

另一種方式是將陣列變成 array[x][y]的型式 ,並以函數的方式去讀檔

namespace Noise_Removal_Csharp
{
     class Program_Main
     {
          static void Main(string[] args)
          {
                 const string baseline_filename = "baseline_eeg_data.txt";
                 const int channel_num = 32;
                  // ****** Step 1: Read Data ******
                double[][] baseline_eeg = read_file(baseline_filename, channel_num);
           }

           public static double [][] read_file(String filename, int channel_num)
           {
                string temp_file = System.IO.File.ReadAllText(filename);
                char[] separators = { ' ', '\n', '\r', '\t', ',', ',', '!' };
                string[] tempArray_file = temp_file.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                int count_array_num = 0;
                foreach (string tmp in tempArray_file)
                {
                      count_array_num++;
                }
                double[][] file_array = new double[channel_num][];
                int temp_col=(count_array_num / channel_num);
                for (int temp_i = 0; temp_i < channel_num; temp_i++)
                {
                      file_array[temp_i] = new double[temp_col];
                }

                int i, j, k = 0;
                for (i = 0; i < (channel_num); i++)
                {
                      for (j = 0; j < (count_array_num / channel_num); j++)
                      {
                           file_array[i][j] = double.Parse(tempArray_file[k]);
                           k++;
                      }
                }
                return file_array;
           }
      }
}

 

arrow
arrow
    文章標籤
    C#
    全站熱搜

    HuangJung1216 發表在 痞客邦 留言(0) 人氣()