C#读取txt文本文件中的数据

原始数据文件是txt文件,存储的方式如下:
-----2012年01月01日温度_T=19 资料----
----- -00 -01 -02
57206 6.4(6.6/6.4) 6.3(6.5/6.3) 6.3(6.3/6.3)
57204 4.4(4.6/4.4) 4.3(4.4/4.3) 4.3(4.3/4.3)
57217 6.2(6.5/6.2) 5.8(6.2/5.8) 5.1(5.8/5.0)
57208 5.5(5.6/5.5) 5.4(5.5/5.4) 5.3(5.4/5.3)
57303 -6.2(-6.5/-6.2) -6.1(-6.2/-6.0) -5.9(-6.1/-5.9)
56196 6.0(6.0/5.9) 5.9(6.0/5.9) 5.9(5.9/5.9)
56193 5.8(6.0/5.7) 5.6(5.8/5.6) 5.4(5.6/5.4)
56194 5.6(5.7/5.5) 5.5(5.6/5.5) 5.2(5.5/5.2)

现在我需要用C#语言将第3行到第10的数据读取出来存入数组里面,然后再求平均值。我目前遇到的难点有3点:
第一行和第二行可以不用读取,那如何跳过前面两行,直接从第三行开始读取。
第三行到第十行里面的每个数据如何读取,我搜了好多资料,讲的都是一整行一整行的读取,而我需要得到其中的每个数据。
里面带负号的数据又如何来读取。
我刚刚接触C#语言,好多东西都不是很懂,而且老板又催的很紧,所以就只有以发帖的方式请教大神了。

1、首先先来看一下准备的txt文本文件的内容。

2、然后在程序中引入操作文件的命名空间System.IO。

3、接下来需要定义一个变量,存储文件所在的路径。

4、然后先读取文本内容,调用File类的ReadAllLines即可读取所有内容。

5、接下来是写入内容,按照下图的方式,准备要写入的内容。

6、准备好内容以后,调用File的WriteAllLines进行内容的写入。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-12-16
List<string> listLines = new List<string>();
using(StreamReader reader = new StreamReader ("your text file's path"))
{
    int i = 1;
    string line = reader.ReadLine();
    while(line!="" && line != nul)
    {
        if(i>=3)
        {
            listLines.Add(line);
        }
        line = reader.ReaderLine();
    }
    //循环完后,listLines 里面就放有第三行到第十行的数据了
}
for(int i = 0; i< listLines.Count ; i++)
{
    //listLines[i] 你想怎么处理就怎么处理咯
}

追问

谢谢您,刚刚按着您写的,现在是解决了第一个问题,就是把第三行到第十行的数据存到listLines这个里面了,那接下来,每一行里面有括号,有空格,有负号,这些又如何来处理呢?

追答List<string> listLines = new List<string>();
using(StreamReader reader = new StreamReader ("your text file's path"))
{
    int i = 1;
    string line = reader.ReadLine();
    while(line!="" && line != nul)
    {
        if(i>=3)
        {
            listLines.Add(line);
        }
        line = reader.ReaderLine();
    }
    //循环完后,listLines 里面就放有第三行到第十行的数据了
}
for(int i = 0; i< listLines.Count ; i++)
{
    string temp = listLines[i];
    //string temp = "57206 6.4(6.6/6.4)       6.3(6.5/6.3)       6.3(6.3/6.3)";
    int numberLength = temp.IndexOf(' ')+1;
    int tempLength = temp.Length;
    string number1 = temp.Substring(0, numberLength);
    
    temp = temp.Substring(numberLength, tempLength - numberLength);
    numberLength = temp.IndexOf('(');
    tempLength = temp.Length;
    string number2 = temp.Substring(0, numberLength);
    temp = temp.Substring(numberLength, tempLength - numberLength);
    //...................后面的其他数自个慢慢调吧
}

追问

非常谢谢您,我大致了解您的编程想法了

追答

那就照着处理下去就好了啊,你可以看到有好多重复代码,其实可以用一个方法来处理的

本回答被网友采纳
第2个回答  2015-08-22

下面的控制台应用程序实现要求的功能

using System;
using System.IO;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 新建一个DataTable
            DataTable tb = new DataTable();
            // 添加一列用于存放读入的浮点数
            DataColumn c= tb.Columns.Add("Value", typeof(double));

            // 打开文件准备读取数据   
            StreamReader rd = File.OpenText(@"d:\data.txt");
            string line;
            // 循环读出文件的每一行
            while ((line = rd.ReadLine()) != null)
            {
                // 拆分出一行的所有用空格分割的数据项
                string[] values = line.Split(' ');
                // 将每个数据项转换成浮点数,并存入DataTable
                foreach (string s in values)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        // 转换成浮点数
                        double v = double.Parse(s);
                        // 存入DataTable
                        DataRow r = tb.NewRow();
                        r["Value"] = v;
                        tb.Rows.Add(r);
                    }
                }
            }
            rd.Close();
             //输出DataTable中保存的数组
            foreach (DataRow r in tb.Rows)
            {
                Console.WriteLine(r["Value"]);
            }
        }
    }
}

D:\Data.Txt内容为

程序运行结果

相似回答