Reading specific parts of text from a text file

Hi, I’ve been thinking about this for a few days now but I still haven’t came up with a solution.
So I basically want the player to be able to write some text and then have it saved, Then at some other point I want to be able to load that text and have the player be able to read it.

I am currently able to save text to file and read the text and display it. I also know how to split strings to use as headings and such.

What I am having trouble doing is getting chunks of text from a long file. So if I only want to load a paragraph of text by searching for a heading or something.
For example if the player were to make a diary entry and then save the paragraph to a text file with the heading of the date. Later on the player would load the text from the text file and display it.

The main issue I’m having is figuring out how to have multiple paragraphs in the text file and being able to search for them using the heading.

I know I could go with something like:

string[] textContent = File.ReadAllLines(notesTextFile);

        for (int i = 0; i < textContent.Length; i++)
        {
            if (textContent [i].Contains (headingString))
            {
               
            }
        }

Then I could basically say read and display the text from the index’s greater than this index.

But then how would I tell it to stop reading the text? like at the end of a paragraph?

Any info would be awesome. Though I do have a suspicion that I may be going about this the wrong way,

Thanks!

I like using the StreamReader class when searching for a block of text in a file, since it allows me to read a line at a time.

Something like this:

           List<string> lines = new List<string>();

               using (var reader = new StreamReader(fileName))
               {
                   var startFound = false;

                   while (!reader.EndOfStream)
                   {
                       var line = reader.ReadLine();

                       if (line.IsNullOrWhiteSpace()) continue;

                       if (!startFound) startFound = line.Contains(startSearchString);

                       if (startFound)
                       {
                           var isEnd = line.Contains(endSearchString);

                           if (!isEnd || includeEndString) lines.Add(line);

                           if (isEnd) break;
                       }
                   }
               }

Alternatively, the FileStream class lets you jump straight to a specific position in the file using FileStream.Position, and read in a chunk as byte data.

Ahh I may have to look in to this. From what I can see in your code is basically a footer called isEnd and when the loop reads that it stops. So in my case I would basically have to add a string to the end of the paragraph when saving the file. and then loop through each line until it finds isEnd. I never thought to check for a second string while reading through the lines of my text.

Thanks. Just needed to get my head around it!! :stuck_out_tongue:

Well, you could always define an exit condition as something like N lines, how large the block is you want to read, etc.

This is just one way to do it, after all :slight_smile:

If you’re dealing with text files that are gigabytes in size (lol), then using a header to tell you where to jump to might be better.