Read a character delimited text file with one header and multiple detail lines in C# efficiently

86 views Asked by At

I need to read a character (^) delimited text file line by line and replace incorrect data in the same file. My current program does this, but I would like to know if there is a more efficient way to do this, if huge text files need to be processed.

Input File data:

10^I^23231^LGGE^DTL^09595~
20^00001^11^0.03000^0.05000^0.40500^EXETER~
30^223312^R^700^12.90^10^~
40^R^70036700015^01~
50^00000^12~
30^000000^C^781^11.10^09~
40^R^70036700016^01~
50^00000^12~
90^007^20231208^090909^0001^11~

Line number 10 indicates the header, 90 trailer and everything in between are the detail lines, with each line number corresponding to a particular attribute like contract detail or order detail or supplier detail.

My code looks for incorrect supplier detail (i.e., line number 30) and checks if the supplier number in column 2 is valid. If it is 000000, it is replaced by a default value "221212". This process is repeated in loop for the next line number 30 and repeated till EOF. The rows have different number of entities but rows with same starting line number have the same number of entities. The StreamReader class does work but I'm wondering if there is a more efficient solution available. Tried FileHelpers dll but couldn't get it to work as expected to fetch and replace the exact value.

Code:

public chkfile()
{
    string[] ipFields = null;
    ArrayList nextpos = new ArrayList();
    StreamReader reader = new StreamReader("inputfile.txt");

    nextpos.Add("30");
    string res = string.Empty;

    while (reader.Peek() >= 0)
    {
        fileLine = reader.ReadLine().Trim();
        lineLength = fileLine.Length;
        int lineLength_a = fileLine.Length - 1;

        // Gets the ipFields from the file
        recordType = fileLine.Substring(0, 2);
        ipFields = fileLine.Substring(0, lineLength).Split
          (Configuration.ipFile.ColumnDelimiter.ToCharArray());

        if (fileLine.StartsWith("30") && nextpos.Contains("30"))
        {
            for (int i = 0; i < ipFields.Length; i++)
            {
                if (ipFields[i].Equals("000000"))
                {
                    ipFields[i] = ipFields[i].Replace(ipFields[i], "221212");
                }

            }
            res = string.Join(Configuration.ipFile.ColumnDelimiter, ipFields);

            ipFileContent = ipFileContent.Replace(fileLine, res);

            // Set the next expected record type
            nextpos.Clear();
            nextpos.Add("40");
        }
    }
}

public class ipFile
{
    /// <summary>
    /// Value that delimits the Rows
    /// </summary>
    public static string RowDelimiter
    {
        get { return "~"; }
    }

    /// <summary>
    /// Value that delimits the ipFields
    /// </summary>
    public static string ColumnDelimiter
    {
        get { return "^"; }
    }
}
0

There are 0 answers