Search code examples
c#programming-languages

C# Text Reader and Writer


I have a text file here, the text is arranged in columns. What i need to do is read the text file in, format it so that each column starts on a new line. After the file has been read and formatted it needs to be written to a new file.... How would i go about doing this.

The file that need to be inputed looks like this:

Operator ID = BTA020

Name = ASIA CHAMBEGA
Active profile = User_Disabled
Enable status = Disabled
Re-enable date = 31/12/36 00:00:00
Approval status = Approved
Last changed = 21/07/10 07:34:30
Last sign-on = 13/06/08 14:09:37
Calculated pwd = BD
One-time password = No
Assigned unit = None

Operator ID = BTAC002

Name = A KALATA (NBC)
Active profile = User_Disabled
Enable status = Disabled
Re-enable date = 31/12/36 00:00:00
Approval status = Approved
Last changed = 31/05/10 14:04:41
Last sign-on = n/a Calculated pwd = B9
One-time password = No
Assigned unit = None

The output fie should look like this:

Operator ID BTA020, Name ASIA CHAMBEGA, Active profile User_Disabled, Enable Status Disabled, Re-enable date 31/12/ Operator ID BTAC002, Name A Kalata (NBC), Active profile User_Disabled, Enable Status Disabled

// and so forth for 149 records

How would i get the output to look like this?

Thanks Tren


Solution

  • You need a simple converter. Just read complete file (its not big in you case, so it can be read into memmory). And when replase '=' with ' ' and '\n' with ','. Transfromation is complete! After that, resulting text should be writen into new file. thats all!

    code should be like this:

            string t = File.ReadAllText(path);
    
            t = t.Replace('=', ' ');
            t = t.Replace("\r\n\r\n", ",");
            t = t.Replace("\r\n", ",");
            t = t.Replace("\n", ",");
            t = t.Replace("  ", " ");