码迷,mamicode.com
首页 > 编程语言 > 详细

C#读txt文件并写入二维数组中(txt数据行,列未知)

时间:2014-10-21 15:07:48      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   使用   for   sp   

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

namespace 二维数组
{
    class Program
    {
        static void Main(string[] args)
        {
            int row = 0;//
            int col = 0;//lie
            FileStream fs;
            string path = @"C:\Documents and Settings\Administrator\桌面\1.txt";
            fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader sr;
            sr = new StreamReader(fs);
            List<double[]> arrary = new List<double[]>();  

            while (!sr.EndOfStream)
            {
                string[] arr = sr.ReadLine().Split( );
                col = arr.Length;
                double[] temp=new double[col];
                for (int x = 0; x < col; x++)
                {
                    temp[x] = Convert.ToDouble(arr[x]);
                }
                arrary.Add(temp);
                for (int y = 0; y < col; y++)
                {
                    Console.Write(arrary[row][y] + " ");
                }
                row++;
                Console.WriteLine("\n");
            }
            sr.Close();
            fs.Close();
            Console.Read();
        }
    }
}

arrary定义的泛型 double数组类型。是二维数组的使用方法。可以通过arrary[x][y],进行数据的读取 .注意必须先add 每次add一次 ,x就加1.

如果arrary这样定义 

 List<double[,]> arrary = new List<double[,]>();   那么读取数据应该这样arrary[num][x,y] 每次add一次 num都会+1, add赋值时也必须是[,]的对象。

C#读txt文件并写入二维数组中(txt数据行,列未知)

标签:style   blog   color   io   os   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/zhayunjia/p/4040151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!