每天结束时会生成 2 份报告。我希望能够在每个月底对它们进行分析。
每个文件里面都有一千行左右的长 ASCII 字符串。这已成功编码,但一次只能提取和执行一个文件的分析。
我将尝试在下面放入代码的相关结构。我希望这足以让您了解需要做什么。如果没有,我很乐意发布整个内容。
using (StreamReader reader = new StreamReader("YYYYMMDD----1234D.dat"))
{
while loop //this goes through all the lines in the file.
{if //if meets certain criteria then store into a list, otherwise ignore
}
foreach // this part does the analysis of all the values in the list, totals, etc
.
第一个报告是上述格式,另一个有不同的数字代替 1234D(为了参数,5678D,所以:yyyymmdd----5678D)。这两个数字始终保持不变。
我希望能够将每个文件中的所有数据都存储到我的列表中,然后对整个月进行分析,而不是每天进行细分,所以当它到达文件末尾,将名称增加一天,循环遍历该名称等(或查找所有带有 X 月的文件 - 以更好者为准)。这将填充列表,然后 foreach 将执行其分析和输出。预计我会将所有需要的文件放入与程序当前使用的文件夹相同的文件夹中。
当前代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
class Program
{
public class EntryLine
{
public int I { get; set; }
public string LineStart { get; set; }
public string Letters { get; set; }
public int TVolume { get; set; }
public long TPrice { get; set; }
public double MVolume { get; set; }
public string Currency { get; set; }
public string DateTime {get; set; }
}
static void Main(string[] args)
{
List<EntryLine> myList = new List<EntryLine>();
int i = 1;
using (StreamReader reader = new StreamReader("20121203----1234D.dat"))
{
string line;
var locations = new Dictionary<string, int[]>() {
{"210", new [] {405, 4, 128, 12, 141, 12, 247, 15, 121, 3}},
{"310", new [] {321, 4, 112, 12, 125, 12, 230, 15, 105, 3}},
{"410", new [] {477, 4, 112, 12, 125, 12, 360, 15, 105, 3}}
};
while ((line = reader.ReadLine()) != null)
{
var lineStart = line.Substring(0, 3);
if (lineStart == "210" || lineStart == "310" || lineStart == "410")
{
var currentLocations = locations[lineStart];
var letters = line.Substring(currentLocations[0], currentLocations[1]);
var tvolume =
int.Parse(line.Substring(currentLocations[2], currentLocations[3])) +
int.Parse(line.Substring(currentLocations[4], currentLocations[5]));
var tprice = long.Parse(line.Substring(currentLocations[6], currentLocations[7]));
var mvolume = tprice * tvolume * 0.01 * 0.0000001;
var currency = line.Substring(currentLocations[8], currentLocations[9]);
myList.Add(new EntryLine()
{
I = i,
LineStart = lineStart,
Letters = letters,
TVolume = tvolume,
TPrice = tprice,
MVolume = mvolume,
Currency = currency
});
i = i + 1;
}
}
var x = myList.GroupBy(g => new { g.Letters, g.Currency })
.Select(a => new { a.Key.Letters, a.Key.Currency, TSum = a.Sum(s => s.TVolume), MSum = a.Sum(s => s.MVolume) });
foreach (var item in x)
{
Console.WriteLine("{0} currency: {1} tvolume: {2} mVolume: {3}", item.Letters, item.Currency, item.TSum, item.MSum);
}
} Console.ReadLine();
}
}