?
Solved

Reading and parsing chunks of data to end of file

Posted on 2006-07-06
15
Medium Priority
?
252 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
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…

765 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