我想解析另一个格式不同的文本文件。
两种格式是:
2018-02-05 00:00:00.023,1C2,00006590000002A1
和
1) 2.0 Rx 0400 8 01 5A 01 57 01 D2 A6 02
当前读取第一种格式,但是我也希望读取第二种格式。
public LogConverter()
{
_Pack = new Pack();
Stopwatch = new Stopwatch();
}
public void Start(Func<string, DateTime> ConvertTimestamp)
{
Stopwatch.Start();
var task = new Task(() =>
{
int counter = 0;
var delimiter = new char[] { ',' };
var messageData = new byte[8];
var logTimestamps = new List<DateTime>();
CanDataLog log = null;
foreach (var filePath in _FilePaths)
{
FileTimeRanges.Add(filePath, null);
using (StreamReader sr = new StreamReader(filePath))
{
var timestamp = DateTime.MinValue;
while (!sr.EndOfStream)
{
try
{
var line = sr.ReadLine();
var fields = line.Split(delimiter);
Progress = (int)(100 * (sr.BaseStream.Position / (double)sr.BaseStream.Length));
log = new CanDataLog();
log.Timestamp = ConvertTimestamp(fields[0]);
logTimestamps.Add(log.Timestamp);
// Ignore the Type field
// log.Type = Convert.ToByte(fields[1]);
if (timestamp.AddSeconds(SampleRate) < log.Timestamp)
{
log.Id = Convert.ToUInt16(fields[1], 16);
var messageDataString = fields[2].PadRight(16, '0');
log.Data[0] = Convert.ToByte(messageDataString.Substring(00, 2), 16);
log.Data[1] = Convert.ToByte(messageDataString.Substring(02, 2), 16);
log.Data[2] = Convert.ToByte(messageDataString.Substring(04, 2), 16);
log.Data[3] = Convert.ToByte(messageDataString.Substring(06, 2), 16);
log.Data[4] = Convert.ToByte(messageDataString.Substring(08, 2), 16);
log.Data[5] = Convert.ToByte(messageDataString.Substring(10, 2), 16);
log.Data[6] = Convert.ToByte(messageDataString.Substring(12, 2), 16);
log.Data[7] = Convert.ToByte(messageDataString.Substring(14, 2), 16);
_Pack.TransformMessage(log);
MessageReady();
timestamp = log.Timestamp;
if (++counter == ChunkRate && ChunkReady != null)
{
counter = 0;
ChunksCount++;
ChunkReady(logTimestamps.Min());
}
}
}
catch
{
// Skip comments and errors
}
}
if (logTimestamps.Count > 0)
{
var startDate = logTimestamps.Min();
var endDate = logTimestamps.Max();
FileTimeRanges[filePath] = new Tuple<DateTime, DateTime>(startDate, endDate);
logTimestamps.Clear();
logTimestamps.Add(startDate);
}
}
}
Stopwatch.Stop();
if (counter > 0 && ChunkReady != null)
{
counter = 0;
ChunksCount++;
ChunkReady(logTimestamps.Min());
logTimestamps.Clear();
}
Completed?.Invoke();
});
task.Start();
}
这是当前读取文本文件的内容。我将如何使其能够读取第二种格式?一个文件是.txt,另一个文件是.trc。第二种格式没有日期和时间也是一个问题吗?任何帮助将不胜感激。我是编码的新手,所以尽可能多的信息会很棒。