Overwriting class object due to string parsing issue
I am trying to read a fixed width text file and store each element to a
class object. Each row in the text file needs to be checked for which
parsing action must be performed. Rows 1000 and 1001 each need to be
parsed differently and rows 1002-1004 use the same parsing action. I am
reading the file and parsing correctly, but when I add elements to the
class object the object gets overwritten in each "if" statement. Then when
the object is added to a list the list contains empty lines. I feel like I
am taking the wrong approach. I am new to C# and I apologize if this has
been answered, but after many hours of searching I cannot find a solution.
Below is the text file and code.
1000 AttributeOne AttributeTwo
1001 123456.000000 AttributeFour
1002 1234.0 2345.0 3456.0 1003 1234.0 2345.0 3456.0 1004 1234.0 2345.0
3456.0 1000 AttributeOne AttributeTwo
1001 456789.000000 AttributeFour
1002 1234.0 2345.0 3456.0 1003 1234.0 2345.0 3456.0 1004 1234.0 2345.0
3456.0 1000 AttributeOne AttributeTwo
1001 234567.000000 AttributeFour
1002 1234.0 2345.0 3456.0 1003 1234.0 2345.0 3456.0 1004 1234.0 2345.0
3456.0 1000 AttributeOne AttributeTwo
1001 345678.000000 AttributeFour
1002 1234.0 2345.0 3456.0 1003 1234.0 2345.0 3456.0 1004 1234.0 2345.0 3456.0
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
namespace list_question
{
public class someObject
{
//Line 1
public int Line1_ID;
public string Line1_Attribute1;
public string Line1_Attribute2;
//Line 2
public int Line2_ID;
public decimal Line2_Attribute3;
public string Line2_Attribute4;
//Line 3
public int Line3_ID;
public decimal data_pos1;
public decimal data_pos2;
public decimal data_pos3;
}
class Program
{
static void Main(string[] args)
{
string filepathRead = @"C:\Users\C121736\Desktop\SampleFile.txt";
List<someObject> somObList = new List<someObject>();
someObject obj = new someObject();
try
{
FileStream fs = new FileStream(filepathRead,
FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if ((line.Substring(0, 4) == "1000"))
{
string[] strArrFields1 = Regex.Split(line,
@"(.{6})" + "(.{14})" + "(.{20})").Where(s =>
!string.IsNullOrEmpty(s)).ToArray();
someObject obj_1 = new someObject();
obj_1.Line1_ID =
int.Parse(strArrFields1[0].TrimEnd(' '));
obj_1.Line1_Attribute1 =
strArrFields1[1].TrimEnd(' ');
obj_1.Line1_Attribute2 =
strArrFields1[2].TrimEnd(' ');
somObList.Add(obj_1);
}
else if ((line.Substring(0, 4) == "1001"))
{
string[] strArrFields2 = Regex.Split(line,
@"(.{6})" + "(.{20})" + "(.{15})").Where(s =>
!string.IsNullOrEmpty(s)).ToArray();
someObject obj_1 = new someObject();
obj_1.Line2_ID =
int.Parse(strArrFields2[0].TrimEnd(' '));
obj_1.Line2_Attribute3 =
decimal.Parse(strArrFields2[1].TrimEnd(' '));
obj_1.Line2_Attribute4 =
strArrFields2[2].TrimEnd(' ');
somObList.Add(obj_1);
}
else
{
string[] strArrFields3 = Regex.Split(line,
@"(.{6})" + "(.{7})" + "(.{7})" +
"(.{7})").Where(s =>
!string.IsNullOrEmpty(s)).ToArray();
someObject obj_1 = new someObject();
obj_1.Line3_ID =
int.Parse(strArrFields3[0].TrimEnd(' '));
obj_1.data_pos1 =
decimal.Parse(strArrFields3[1].TrimEnd(' '));
obj_1.data_pos2 =
decimal.Parse(strArrFields3[2].TrimEnd(' '));
obj_1.data_pos3 =
decimal.Parse(strArrFields3[3].TrimEnd(' '));
somObList.Add(obj_1);
}
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Program Exited");
Environment.Exit(99);
}
foreach (someObject o in somObList)
{
Console.WriteLine("{0},{1},{2}\n{3},{4},{5}\n{6},{7},{8},{9}",
o.Line1_ID, o.Line1_Attribute1, o.Line1_Attribute2,
o.Line2_ID, o.Line2_Attribute3, o.Line2_Attribute4,
o.Line3_ID, o.data_pos1, o.data_pos2, o.data_pos3);
}
somObList.Clear();
}
}
}
No comments:
Post a Comment