?
Solved

I NEED TO READ AND WRITE INTO TEXT FILE. HOWEVER, I ONLY NEED TO READ A SECTION OF THE TEXT FILE AT A TIME.

Posted on 2003-03-19
13
Medium Priority
?
215 Views
Last Modified: 2010-04-15
Calling the experts out there.
I know how to read and write into a text file.
What I need to do is how to read ONLY a portion of the text file.

say my text file is temp.txt, and it contains the following text:

/section1/
these are the contents
and more contents
.............
/end section1/

/section2/
these are the contents
and more contents
.............
/end section2/

/section3/
these are the contents
and more contents
.............
/end section3/

How do I read only a section at a time? I only want the contents of section1 to appear in a textbox. How do I do that?
I know there should be a way to tell the program to stop reading when it gets to a certain string, but I don't know how to do it.

Thanks in advance.

Dave

0
Comment
Question by:innovator2
[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
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 10

Expert Comment

by:smegghead
ID: 8167841
This will work

          private string ReadSection(string FileName,int Section)
          {
               string ThisLine;
               string StrSection;
               string Result;
               bool Reading;
               StreamReader MySR;
               StrSection=Section.ToString();
               MySR=File.OpenText(FileName);
               Reading=false;
               Result="";
               while (MySR.Peek()!=-1)
               {
                    ThisLine=MySR.ReadLine();
                    if (ThisLine=="/end section"+StrSection+"/") break;
                    if (Reading) Result+=ThisLine+"\r\n";
                    if (ThisLine=="/section"+StrSection+"/") Reading=true;
               }
               return Result;
          }
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8167894
Damn you're fast, smegghead... ;-)

But the code could be quite better in style - for instance, using string constants for start- and endsection checks as well as a stringbuilder instead of string concats would make the whole thing much faster with large files and sections.
0
 
LVL 10

Expert Comment

by:smegghead
ID: 8167987
Ahhh, we meet again. :-)

Only trying to point him/her in the right direction.. not trying to write commercial/highly efficient software.. otherwise by the time I've put it through QA, innovator might have given up on this.

Using StringBuilder is only more efficient if, as you say, there are more than 5 or 6 concatenations. And as we don't know from his question... your guess is as good as mine...

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

Expert Comment

by:AvonWyss
ID: 8168449
You're right, but as you might have noticed, I am quite a freak when it's about elegant and efficient solutions...

See you in anouther Q, right? Have a nice evening! ;-)
0
 
LVL 10

Expert Comment

by:smegghead
ID: 8168480
ta, you too. You based in UK ??
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8168505
No, I'm living in little Switzerland....
0
 
LVL 1

Expert Comment

by:tpatten
ID: 8171015
Ahhhh. Switzerland. A peace loving country...
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8173654
AvonWyss---just a simple Switzerland question---

Is the plan to defend the America's Cup, in three years, on Lake Geneva?  LOL

Cheers from the US

AW
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8173732
Nah, I guess we'll just make an artificial sea or something... *g*
0
 

Author Comment

by:innovator2
ID: 8178729
Hi all,
Thank you for your inputs.
Please I want the best solution to this problem.

smegghead said:
"Using StringBuilder is only more efficient if, as you say, there are more than 5 or 6 concatenations. And as we don't know from his question... your guess is as good as mine... "

As a matter of fact, there are between 4 and 35 lines in each section, and there are possibly 8 sections in each file. Going by this info, we may say that a more efficient solution is preferred.
Please help me write an efficient software; I am even willing to double the points so that both experts can split them.

And, lest you all forget me, please let's solve this first before we learn about Switzerland-:)

Dave
0
 
LVL 10

Accepted Solution

by:
smegghead earned 600 total points
ID: 8179737
New, speeded up version.


          private string ReadSection(string FileName,int Section)
          {
               string ThisLine;
               string StartSection,EndSection;
               StringBuilder Result=new StringBuilder("",200); // 200 being the initial capacity of the string (this is a guess on my behalf)
               bool Reading;
               StreamReader MySR;
               StartSection="/section"+Section.ToString()+"/";
               EndSection="/end section"+Section.ToString()+"/";
               MySR=File.OpenText(FileName);
               Reading=false;
               while (MySR.Peek()!=-1)
               {
                    ThisLine=MySR.ReadLine();
                    if (ThisLine==EndSection) break;
                    if (Reading) Result.Append(ThisLine+"\r\n");
                    if (ThisLine==StartSection) Reading=true;
               }
               return Result.ToString();
          }
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8179924
If speed is really an issue, you could read all sections in one go instead of reparsing the whole file for each section.

But if you want this, you should post it in an new Q - I believe this Q was answered more than adequately by smegghead!
0
 

Author Comment

by:innovator2
ID: 8179990
Great!
Thanks for your time and efforts.

Now, let's resume the Switzerland talk......
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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