Solved

Parsing through a pipe delimited File

Posted on 2010-09-23
10
2,132 Views
Last Modified: 2012-05-10
Hello Everyone,

In the sample code I supplied, I and using a StreamReader to read a pipe delimited file. A sample of the file looks like this:

11517838|05   |F |01|2041|Y|0.1500|N| \n 11517838|05   |F |01|2042|Y|0.1500|N| \n11517838|05   |F |01|2044|Y|0.1500|N| \n11517838|05   |F |01|2047|Y|0.1500|N| \n11517838|05   |F |01|2049|Y|0.1500|N| \n

In the file, they are all on one line, with the newline character after the |N|. What I need to be able to do, is read the whole file into a string array like I have shown, but then I need to go 'line' by 'line' to populate some string variables. Right now when I run my code, it will go in and get the first:

11517838|05   |F |01|2041|Y|0.1500|N|

from the file, but when it goes in for the second iteration, It does not move to the next line, aka, the character after the newline mark. Thanks in advance for your help.
StreamReader sr = new StreamReader(dailyAudit.ToString());

                        auditLine = sr.ReadToEnd();

                        string auditContents = auditLine;

                        string[] lines = auditContents.Split(char.Parse("|"));

                        int x = 1;

                        

                        while (x < lines.Length && lines[x].ToString() != "\n")

                       {

                            x++;

                            string MID = lines[0];

                            string Vendor = lines[1];

                            string VendType = lines[2];

                            string CardID = lines[3];

                            string ChargeType = lines[4];

                            string RateType = lines[5];

                            string Amount = lines[6];

                            string ExcessRol = lines[7];

                            string FileDate = ftpDate;

                       }

Open in new window

0
Comment
Question by:Abacus IT
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:Umar Topia
ID: 33744170
Instead of this
you use streamreader to read the file
and then use object of streamreader srObj.ReadLine() to read line by line
0
 
LVL 1

Author Comment

by:Abacus IT
ID: 33744213
So, I could change the code to look like:
StreamReader sr = new StreamReader(dailyAudit.ToString());
                        auditLine = sr.ReadLine();
                        string auditContents = auditLine;
                        string[] lines = auditContents.Split(char.Parse("|"));
                        while (sr.ReadLine() != null)
                       {
                            string MID = lines[0];
                            string Vendor = lines[1];
                            string VendType = lines[2];
                            string CardID = lines[3];
                            string ChargeType = lines[4];
                            string RateType = lines[5];
                            string Amount = lines[6];
                            string ExcessRol = lines[7];
                            string FileDate = ftpDate;

                       }

Open in new window

0
 
LVL 1

Author Comment

by:Abacus IT
ID: 33744287
Edit to that last code sample:

while (sr.ReadLine() != null)
                       {    
                            string auditContents = auditLine;
                            string[] lines = auditContents.Split(char.Parse("|"));
                           
                            string MID = lines[0];
                            string Vendor = lines[1];
                            string VendType = lines[2];
                            string CardID = lines[3];
                            string ChargeType = lines[4];
                            string RateType = lines[5];
                            string Amount = lines[6];
                            string ExcessRol = lines[7];
                            string FileDate = ftpDate;

                       }
0
 
LVL 1

Author Comment

by:Abacus IT
ID: 33745052
So I totally changed my code to read as you see below. Problem is that it still won't move on to the next line in the file.
while (sr.ReadLine() != null)
                        {
                            string[] lines = sr.ReadLine().Split(char.Parse("|"));

                            string MID = lines[0];
                            string Vendor = lines[1];
                            string VendType = lines[2];
                            string CardID = lines[3];
                            string ChargeType = lines[4];
                            string RateType = lines[5];
                            string Amount = lines[6];
                            string ExcessRol = lines[7];
                            string FileDate = ftpDate;
                        }

Open in new window

0
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 250 total points
ID: 33745554

Use it like below:




while (true)

                        {

string aLine = sr.ReadLine();

if(aLine == null) break;



                            string[] lines = aLine.Split("|");



                            string MID = lines[0];

                            string Vendor = lines[1];

                            string VendType = lines[2];

                            string CardID = lines[3];

                            string ChargeType = lines[4];

                            string RateType = lines[5];

                            string Amount = lines[6];

                            string ExcessRol = lines[7];

                            string FileDate = ftpDate;

                        }

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Accepted Solution

by:
kris_per earned 250 total points
ID: 33745587

I mean, as below:


StreamReader sr = new StreamReader(dailyAudit.ToString());



while (true)

      {

             string aLine = sr.ReadLine();

             if(aLine == null) break;



                            string[] lines = aLine.Split("|");



                            string MID = lines[0];

                            string Vendor = lines[1];

                            string VendType = lines[2];

                            string CardID = lines[3];

                            string ChargeType = lines[4];

                            string RateType = lines[5];

                            string Amount = lines[6];

                            string ExcessRol = lines[7];

                            string FileDate = ftpDate;

                        }

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33745589
You can use File.ReadAllLines to get a string array, where each item in the array represents one line in the file.  Then loop through that array, and split each individual line on the | character to get the fields.
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;



namespace ConsoleApplication1

{

	class Program

	{

		static void Main(string[] args)

		{

			string[] lines = File.ReadAllLines("C:\\test.txt");



			foreach (string line in lines)

			{

				string[] fields = line.Split('|');



				string one = fields[0];

				string two = fields[1];

				string three = fields[2];

				string four = fields[3];

			}

		}

	}

}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33745645
Sorry...didn't catch that your using a StreamReader on a string variable.  If File.ReadAllLines doesn't work in your case, you can just use Split('\n') on dailyAudit.ToString().
...what is dailyAudit?  Maybe the problem is that dailyAudit only has one line in it?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;



namespace ConsoleApplication1

{

	class Program

	{

		static void Main(string[] args)

		{

			string dailyAudit = "The|Quick|Brown|Fox\nJumped|Over|The|Lazy\nDog|Hello|World|Test\n";



			string[] lines = dailyAudit.ToString().Split('\n');



			foreach (string line in lines)

			{

				string[] fields = line.Split('|');



				string one = fields[0];

				string two = fields[1];

				string three = fields[2];

				string four = fields[3];

			}

		}

	}

}

Open in new window

0
 
LVL 1

Author Comment

by:Abacus IT
ID: 33746485
@Kris_per, I tried your code, and it is doing exactly what I need it to. Thank you very much for all the help everyone.
0
 
LVL 1

Author Closing Comment

by:Abacus IT
ID: 33746498
Question was answered exactly as I needed it to be.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 debugging 2 35
C# Offline Apllication 5 56
Resolve Dependency Issues 4 48
Video Player 2017 5 17
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now