Solved

Reading and parsing chunks of data to end of file

Posted on 2006-07-06
15
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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
 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
Progress

718 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