[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Reading and parsing chunks of data to end of file

Posted on 2006-07-06
15
Medium Priority
?
254 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This is about my first experience with programming Arduino.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

649 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