• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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

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
innovator2
Asked:
innovator2
  • 5
  • 4
  • 2
  • +2
1 Solution
 
smeggheadCommented:
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
 
AvonWyssCommented:
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
 
smeggheadCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
AvonWyssCommented:
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
 
smeggheadCommented:
ta, you too. You based in UK ??
0
 
AvonWyssCommented:
No, I'm living in little Switzerland....
0
 
tpattenCommented:
Ahhhh. Switzerland. A peace loving country...
0
 
Arthur_WoodCommented:
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
 
AvonWyssCommented:
Nah, I guess we'll just make an artificial sea or something... *g*
0
 
innovator2Author Commented:
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
 
smeggheadCommented:
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
 
AvonWyssCommented:
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
 
innovator2Author Commented:
Great!
Thanks for your time and efforts.

Now, let's resume the Switzerland talk......
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.

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now