我想问大家一下在C#中怎么读取一个文本文件中的数字呢?譬如那个文本文件中记录了一堆数字……我是一个新手,还没有入门,谢谢大家!假设文本文件data.txt记录了下面读数据:
100 150
125 175
150 200
150 225
150 250
150 325
200 250
250 300

解决方案 »

  1.   

    StreamReader reader = new StreamReader(textPath, Encoding.GetEncoding("GB2312"));
    //textPath文件路径
    string line = reader.ReadLine();
    //line 是读取一行,下面就自己处理了
      

  2.   

    这样一行一行读是把100 150读成了一个string,怎么样才能把它分成两个数字呢?
      

  3.   

    /// <summary>
            /// 读文件到字符串中
            /// </summary>
            /// <param name="sFileName">文件名</param>
            /// <returns></returns>
            public string FromFile(string sFileName)
            {
                string content = "";
                FileStream fs = File.Open(sFileName, FileMode.Open, FileAccess.Read);
                StreamReader r = new StreamReader(fs);
                content = r.ReadToEnd();
                r.Close();
                fs.Close();
                return content;
            }        /// <summary>
            /// 把字符串写入文件(会覆盖同名文件)
            /// </summary>
            /// <param name="fname"></param>
            /// <param name="text"></param>
            /// <returns></returns>
            public bool ToFile(string fname, string text)
            {
                FileStream fs = File.Open(fname, FileMode.Create, FileAccess.ReadWrite);
                StreamWriter w = new StreamWriter(fs);
                w.Write(text);
                w.Close();
                fs.Close();
                return true;
            }
      

  4.   

    string s = "100 150";
    string[] s2 = s.Splite(" ");
    则s2[0]="100" s2[1]="150"
      

  5.   

    先ReadLine,然后Split..string[] s=str.Split(new char []{' '});
      

  6.   

    如果还有可能是多个空格..string[] s=Regex.Split(str.Trim(),@"\s+");
      

  7.   

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;public class MyClass
    {
        public static void Main()
        {
            string path = "C:\\1.txt";//文件所在路径
            StreamReader sr = new StreamReader(path, Encoding.Default);
            List<string> tempList = new List<string>();
            string temp;
            while((temp = sr.ReadLine()) != null)//如果不是空
            {
                tempList.Add(temp.Trim());//除去前后空格,放入List
            }
            sr.Close();
            int count = tempList.Count;//
            int[,] array = new int[count, 2];
            int index = 0;
            Regex regex = new Regex("\\s+");//分割用正则表达式
            string[] tempArray;
            foreach(string s in tempList)
            {
                tempArray = regex.Split(s);
                if(tempArray == null || tempArray.Length != 2)//为空或长度不是2
                {
                    continue;
                }
                array[index, 0] = Convert.ToInt32(tempArray[0]);
                array[index, 1] = Convert.ToInt32(tempArray[1]);
                ++index;
            }
            
            for(int i = 0; i < count; ++i)
            {
                Console.WriteLine("tempArray[0]={0},tempArray[1]={1}",array[i, 0],array[i, 1]);
            }
            Console.ReadLine();
        }
    }