0

I've been out of the coding game for a few years now so I'm pretty rusty - feels like I'm starting from scratch so I apologize if this a rookie question.

I have a text file with line items:

> 076-5000-3ABBOTT                   1998010700019900119971205000
048-0002-8ABINGDON                 1998010700019900119971205000
100-5000-3ABSHER                   1998010700019900119971205000

I also have a layout key:

Blockquote

  1. Location-Code-Id PIC X(10) Value "xxx-xxxx-x"
  2. Location-Name PIC X(25)
  3. Current-Rate-Effective-Date PIC X(6) Value "CCYYMM"
  4. Total-Current-Telecom-Rate PIC 9(5) A 7.00% rate would be 07000
  5. Previous-Rate-Effective-Date PIC X(6) Value "CCYYMM"
  6. Previous-Rate-Ending-Date PIC X(6) Value "CCYYMM"
  7. Total-Previous-Telecom-Rate PIC 9(5) A 7.00% rate would be 07000

The total length of each string is 63. I basically need to split the string up by the PIC X lengths and then convert them to the titles ie. "Location-code-Id," which will ultimately be a column header.

This is what I currently have:

class ReadFromFile
{
    static void Main()
    {
        
        string[] lines = System.IO.File.ReadAllLines(@"C:\Files\test.txt");

        // Display the file contents by using a foreach loop.
        System.Console.WriteLine("Contents of test.txt = ");
        foreach (string s in lines)
        {
            string s = s;
            foreach (char c in s)
            {
                if (s.Length = 10)
                {
                    Console.WriteLine(s.Length);
                }

                else if (s.Length = 35)
                {
                    Console.WriteLine(s.Length);
                }

                else
                {
                    Console.WriteLine(s);
                }
            }
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}

This code doesn't work obviously, and I didn't write it to print out text yet, I just wanted to print out the current length position so that I could go from there. Pretty lost at the moment. I ultimately want to convert this to JSON or CSV. Any help would be appreciated, thanks a ton.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
Mav
  • 37
  • 5
  • you could just do a Split: `var arr = lineFromFile.Split(' ', StringSplitOptions.RemoveEmptyEntries);` you would have two elements in your array. – Andy Aug 17 '20 at 01:50
  • This needs to be split into 7 elements - mirroring the key. I believe what you are saying would split the line item half where there are spaces to create two elements example: element 1 - 076-5000-3ABBOTT , element 2 - 1998010700019900119971205000. It needs to be element 1 - 076-5000-3 (length 10), element 2 - "ABBOTT " (length 25), element 3 - 199801 (length 6), element 4 - 07000 (length 5), element 5 - 199001 (length 6), etc... – Mav Aug 17 '20 at 02:12
  • Use `substring` on each element to get what you want. You basically just wrote the code in your comment above. – Andy Aug 17 '20 at 02:20
  • Quick example: `var locationCode = s.Substring(0, 10); var locationName = s.Substring(10, 25); etc...` – Andy Aug 17 '20 at 02:26
  • 1
    This is [fixed-width text](https://stackoverflow.com/questions/162727/), not CSV and not JSON. – Dour High Arch Aug 17 '20 at 02:44
  • Andy, thank you, that definitely pointed me in the right direction - I was unaware of the substring method *facepalm*. Dour High Arch, I am aware that this is not CSV or JSON, I want to convert it to a JSON object though. I was unaware that the format was called 'fixed-width text' so that's definitely helpful to me. – Mav Aug 17 '20 at 02:52

1 Answers1

0

You can use the substring methods and Parsing to create a new object that you can use LINQ to filter and query on later.

List<string> lines = new List<string>() {
    "076-5000-3ABBOTT                   1998010700019900119971205000",
    "048-0002-8ABINGDON                 1998010700019900119971205000",
    "100-5000-3ABSHER                   1998010700019900119971205000" 
};
            
var data = lines.Select(x => new 
{ 
    Code = x.Substring(0, 10), 
    Name = x.Substring(10, 25), 
    CREDT = DateTime.ParseExact(x.Substring(35, 6), "yyyyMM", CultureInfo.InvariantCulture),
    TCTR = double.Parse(x.Substring(41,5))/1000,
    PREDT = DateTime.ParseExact(x.Substring(46, 6), "yyyyMM", CultureInfo.InvariantCulture),
    PRENDT= DateTime.ParseExact(x.Substring(52, 6), "yyyyMM", CultureInfo.InvariantCulture),
    RPETR = double.Parse(x.Substring(58, 5))/1000
});

// data would look like this as an array,
[
  {
    "Code": "076-5000-3",
    "Name": "ABBOTT                   ",
    "CREDT": "1998-01-01T00:00:00",
    "TCTR": 7.0,
    "PREDT": "1990-01-01T00:00:00",
    "PRENDT": "1997-12-01T00:00:00",
    "RPETR": 5.0
  },
  {
    "Code": "048-0002-8",
    "Name": "ABINGDON                 ",
    "CREDT": "1998-01-01T00:00:00",
    "TCTR": 7.0,
    "PREDT": "1990-01-01T00:00:00",
    "PRENDT": "1997-12-01T00:00:00",
    "RPETR": 5.0
  },
  {
    "Code": "100-5000-3",
    "Name": "ABSHER                   ",
    "CREDT": "1998-01-01T00:00:00",
    "TCTR": 7.0,
    "PREDT": "1990-01-01T00:00:00",
    "PRENDT": "1997-12-01T00:00:00",
    "RPETR": 5.0
  }
]
Jawad
  • 11,028
  • 3
  • 24
  • 37