Solved

Reading and parsing chunks of data to end of file

Posted on 2006-07-06
15
231 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

21 Experts available now in Live!

Get 1:1 Help Now