Solved

Reading and parsing chunks of data to end of file

Posted on 2006-07-06
15
236 Views
Last Modified: 2010-04-17
I have a text file that has multiple records in it. The records are not separated by any delimiter and I know exactly how long each record will be (lets say 100 chars).
I'm grabbing sections out of the first record by parsing it and putting the results into fields like this:

There are 3 parts that I'm not sure how to do (see below). Please help.

StreamReader sr = File.OpenText(OPENFILENAME);
            string contents = "";
            bool finished = false;
            while(!finished)
            {
               contents = sr.ReadToEnd();
                if(line==null)
                {
                    finished = true;
                }
                else
                {
                    string membernum = contents.SubString(0,13);
                    string policynum = contents.Substring(14,24);
                    //etc to end of first record of 100 chars
                    //take the result and export it to a file <-- don't know how to do this
                   // loop using the 2nd, then 3rd etc record of 100 chars throught to the eof <-- don't know how to do this
                  //append the results of each record to a new file <-- don't know how to do this
                }

What I need to do is read to the end of the first record (100 chars) parse it all out, take the result and export it to a file so that I can then begin parsing the next record into fields which I will append to the file that I'm exporting.
0
Comment
Question by:boukaka
  • 8
  • 7
15 Comments
 

Author Comment

by:boukaka
ID: 17051701
corrrection - I need to change the if(line==null) to something that has to do with end of file but that's another matter altogether.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17052084
I always feel uncomfortable when there's no error  checking - you should have a try/catch block to allow you to safely do this. Something like this:

        try
        {
            sw = new StreamWriter(NEWFILENAME);

            using (StreamReader sr = new StreamReader(OPENFILENAME))
            {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    string membernum = contents.SubString(0,13);
                    string policynum = contents.Substring(14,24);
                    //etc to end of first record of 100 chars
                    //take the result and export it to a file <-- don't know how to do this
                   // loop using the 2nd, then 3rd etc record of 100 chars throught to the eof <-- don't know how to do this
                  //append the results of each record to a new file <-- don't know how to do this
                    sw.WriteLine(membernum + "," + policynum + ", whatever else");
                }
                sr.Close();
                sw.Close();
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("File error:");
            Console.WriteLine(e.Message);
        }
0
 

Author Comment

by:boukaka
ID: 17052178
Oh good point, I'll add that in as well - thanks
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17052187
I don't know if you noticed, but the respons actually does what you need, as well as adding the try/catch :-)
0
 

Author Comment

by:boukaka
ID: 17052241
with this code, I had to change "contents" to line but I'm getting an error saying 'string' does not contain a definition for 'SubString'....
0
 

Author Comment

by:boukaka
ID: 17052295
Ok, I made some small adjustments and now I have this which SEEMS to work, but nothing is being written to the new file.

        const string OPENFILENAME = @"c:\test.txt";
        const string SAVEFILENAME = @"c:\test2.txt";

        private void btnOpen_Click(object sender, System.EventArgs e)
        {
            try
            {
               
                StreamWriter sw = new StreamWriter(SAVEFILENAME);
                using (StreamReader sr = new StreamReader(OPENFILENAME))
                {
                    string line="";
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        string membernum = line.Substring(0,13);
                        string policynum = line.Substring(14,24);
                        //etc to end of first record of 100 chars
                        //take the result and export it to a file <-- don't know how to do this
                        // loop using the 2nd, then 3rd etc record of 100 chars throught to the eof <-- don't know how to do this
                        //append the results of each record to a new file <-- don't know how to do this
                        sw.WriteLine(membernum + "," + policynum);
                        MessageBox.Show("file written");
                    }
                    sr.Close();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                // Let the user know what went wrong.
                Console.WriteLine("File error:");
                Console.WriteLine(ex.Message);
            }


        }
0
 

Author Comment

by:boukaka
ID: 17052311
I've tossed out the xml part for now - once this is working .. THEN i'll work on sorting and exporting to xml
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 15

Expert Comment

by:bpmurray
ID: 17052321
Oops - I just copied & pasted from yours - it's a case problem. The method is called Substring. not SubString
0
 

Author Comment

by:boukaka
ID: 17052337
ah yes, sorry, I caught that but.. now for the writing to file thing.. there's nothing in the new file when I open it???

The first line in the test file is this: ISS277015481 K277010377820060501    HANSEN                  MARIE

so the output file should have some stuff.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17052352
You have the message-box display after each line! Delete that, or move it to after the close statements.
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 500 total points
ID: 17052407
XML writing is pretty simple. Just after the new Streamwriter, add:
       sw.WriteLine("<?xml version='1.0' encoding='ISO-8859-1'?>\n<memberdata>");

Then replace the next WriteLine with:
       sw.WriteLine("<member>");
       sw.WriteLine("<membernum>" + membernum + "</membernum>");
       sw.WriteLine("<policynum>" + membernum + "</policynum>");
       ......
       sw.WriteLine("</member>");

Between both close statements, add this:
      sw.WriteLine("</memberdata>");
0
 

Author Comment

by:boukaka
ID: 17052420
Oh crap - missed that - i'm in such a hurry. This is perfect, I wish I could give you more than 500 pts. Thank you.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17052456
Thx  for the points.
0
 

Author Comment

by:boukaka
ID: 17052459
oh, one more thing.. what do you mean by "between both close statements"?
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17052903
Like this :-)

                    sr.Close();
                    sw.WriteLine("</memberdata>");
                    sw.Close();
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

This article will show, step by step, how to integrate R code into a R Sweave document
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

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

23 Experts available now in Live!

Get 1:1 Help Now