Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to make a software that opens a text file and read all paragraphs from the file
and store all paragraphs in different variables. In my program paragraph are 3 fixed in count.

Updated 26-Sep-12 23:25pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Working on the assumption that a paragraph is separated by by a carriage return/line feed (char 13, 10) then it's quite simple.

string filePath = @"c:\temp\doc.txt";
System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
byte[] binaryData = new byte[fs.Length];
fs.Read(binaryData, 0, binaryData.Length);
string text = System.Text.Encoding.ASCII.GetString(binaryData);
string[] paragraphs = text.Split('\r').Select(m => m.Trim('\n')).ToArray();
Deepak_Shukla 27-Sep-12 6:18am
but not working
Stephen Hewison 27-Sep-12 6:29am
The approach is sound. It's just the paragraph delimiter which might be wrong. You need to open you text file up in a hex editor. Look at the data in the file and find the characters which are common to where there are paragraphs and then adjust the code to split the text using that delimiter.
Stephen Hewison 27-Sep-12 6:29am
P.s. if you tell someone something is not working. It might be a good idea to explain how it's not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100