我不給你全部做完,你自己參照這個路子改吧:
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using namespace ConsoleApplication4
{
class Program
{
static void Main(String[] args)
{
if (args.Count() == 0)
{
Console.WriteLine("請以數據文件名路徑作爲參數啓動程序!");
return;
}
String fileName = args[0].Trim();
if (!File.Exists(fileName))
{
Console.WriteLine("文件{0}不存在,請檢查并重新輸入。", fileName);
return;
}
Int32 intWidth = 7; // 整型數據的寬度
Int32 floatWidth = 10; // 浮點數據的寬度
Int32 itemsInLine = 10; // 每行的項數
Int32 index;
using (StreamReader file = File.OpenText(fileName))
{
Int32 NT = GetInteger(file, intWidth);
Int32 NP = GetInteger(file, intWidth);
Int32 DT = GetInteger(file, intWidth);
file.ReadLine();
Console.WriteLine("NT={0}, NP={1}, DT={2}", NT, NP, DT);
for (index = 0; index < 6; ++index)
{
Console.Write("{0},", GetInteger(file, intWidth));
}
Console.WriteLine("{0:F3}", GetFloat(file, floatWidth));
file.ReadLine();
Int32 counter = 0;
List<Double> Z = new List<Double>();
index = 0;
while (index < NP)
{
if (counter < itemsInLine)
{
Z.Add(GetFloat(file, floatWidth));
++counter;
++index;
}
else
{
file.ReadLine();
counter = 0;
}
}
foreach (Double data in Z)
{
Console.Write("{0} ", data);
}
Console.WriteLine("\nZ[]的總項數={0}", Z.Count());
}
}
static Int32 GetInteger(StreamReader file, Int32 width)
{
Char[] array = new Char[width];
if (file.Read(array, 0, width) > 0)
{
return Int32.Parse(new String(array));
}
else
{
return 0;
}
}
static Double GetFloat(StreamReader file, Int32 width)
{
Char[] array = new Char[width];
if (file.Read(array, 0, width) > 0)
{
return Double.Parse(new String(array));
}
else
{
return 0.0;
}
}
}
}
關鍵是那兩個函數,以及在適當時候讀走換行的手法。