Parsing a data file in C#

Status
Not open for further replies.

hillbillybob

In Runtime
Messages
341
Here is a few lines of the source file that I want to read. From these lines I can glean a lot of information, but I need help parsing them. It's kind of a long post.

000000000009/05/060000000039000000503015
000999P000009999999999 000000040000000000000
000651P000009999999999 000000072190000000000
000999P000000100626078 000000041750000000000
000999P000000100380248 000000032610000000000
000652P000009999999999 000002831000000000000

Here is how it breaks down (other than the first line, which is a header)
Ex. second line
|000999|P|000009|999999999| |000000040000000000000|
Ex. Third Line
|000651|P|000009|999999999| |000000072190000000000|
Ex. fourth line
|000999|P|000000|100626078| |000000041750000000000|

Psuedo code for this module that I am having trouble with goes:

Open file
Open buffered file reader
Open buffered file writer
Assign header line to array
Assign other line(s) to array
Read header array
loop through other line(s)
read other line(s) array
split 1st block of array into company string
split 4th block of array into customer string
split 6th block of array into amount string
if company == 000999 and customer != 999999999
write company, customer, amount to file

And here is my source code that I have so far.

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace File_Writer
{
    class TextWrite
    {
        public static void Main()
        {
            //make an instance and run it
            TextWrite t = new TextWrite();
            t.run();
            Console.ReadLine();
            
        }
        //set it running with a directory name
        private void run()
        {
            
            String theDirectory = Environment.GetEnvironmentVariable("UserProfile");
            
            //open a file
            FileInfo theSourceFile = new FileInfo(theDirectory + @"\Desktop\0999.cm090506.txt");

            //create text reader for that file
            StreamReader reader = theSourceFile.OpenText();

            //create a text writer to the new file1
            StreamWriter writer = new StreamWriter(theDirectory + @"\Desktop\output.txt", false);

            //create buffer to hold characters
            char[] header = new char[41];
            char[] line = new char[53];
            //read first line, header of file, and essentially dispose of it.
            reader.Read(header, 0, header.Length);
            
            //walk the file, picking apart the different columns
            while (reader.Peek() >= 0)
            {
                //read line
                reader.Read(line, 0, line.Length);
                //split array into more managable strings
                String company = new string(line, 0, 7);
                String waste1 = new string(line, 7, 1);
                String waste2 = new string(line, 8, 6);
                String customer = new string(line, 14, 9);
                String waste3 = new string(line, 23, 9);
                String amount = new string(line, 32, 21);
                //write just the string we want
                writer.Write(company + "," + customer + "," + amount);
            }
            //tidy up
            writer.Flush();
            reader.Close();
            writer.Close();
        }
    }
}

Now, here are some of my problems. When I go to write the conditional that should be in the while loop, testing if company == 000999 and customer != 999999999, I don't get any output. With the source code above, this is a sample of the output I get.

000999,999999999,000000040000000000000
00065,999999999, 000000072190000000000
0009,001003161, 000000036330000000000
000,000100350, 000000047680000000000
00,000010126, 000000028850000000000
0,000001008, 000000090000000000000
,000000100, 000000035780000000000
,P00000010, 000000021050000000000,9P0000001, 00000003255000000000,99P000000, 0000000309400000000,999P00000,7 000000046550000000,0999P0000,77 00000008121000000,00999P000,908 0000001183900000,000999P00,9999 000000021050000,
000999P0,95728 00000003091000,

Except for the conditional not producing any output, the first line looks good. After that, it gets awry. You can see it in the txt file produced, but not here - there are odd characters at the beginning of the first line, and then sprinkled about. The odd characters look like squares or rectangles. I have no control over the source file's format. ( the text file I want to read) Is my logic wrong? Am I missing some lines of code that would prevent the awry output? I think those odd characters are throwing off my array count, but I'm not certain. I tried googling to see if any of those were it. Nothing came up. Ultamatly I will use the Customer string to query a database, matching it to a company, and writing the Company, Customer, and Amount strings to a .CSV file for later use.

Thanks in advance.

P.S. if you need, PM me with your email address, and I will send the text files for you to see.
 
You don't need to work with char arrays. Just use strings. This will work if you make the following changes:

Code:
// get rid of these two lines
char[] header = new char[41];
char[] line = new char[53];

// use this line instead
string header, line;

Code:
// change the way your reader works.  Do this
line = reader.ReadLine();

while (line != null)
{
    string company = line.Substring(0,7);
    string customer = line.Substring(14,9);
    string amount = line.Substring(32);
    writer.WriteLine(company + "," + customer + "," + amount);
    line = reader.ReadLine();
}
 
Status
Not open for further replies.
Back
Top Bottom