rtf and streams
-
Hello!! I have managed to load an rtf file in a streamreader : "StreamReader sr = new StreamReader("C:\\test.rtf");" and display it in a richtextbox: "richTextBox1.Rtf = sr.ReadLine();" Is there any way that I could posibly "read" each ,lets say, 10 lines? I suppose I could detect where this "\par" appears in the stream but don't know how to do it... Any suggestions??? Does the readline method have any operation for this? Thanks!!
-
Hello!! I have managed to load an rtf file in a streamreader : "StreamReader sr = new StreamReader("C:\\test.rtf");" and display it in a richtextbox: "richTextBox1.Rtf = sr.ReadLine();" Is there any way that I could posibly "read" each ,lets say, 10 lines? I suppose I could detect where this "\par" appears in the stream but don't know how to do it... Any suggestions??? Does the readline method have any operation for this? Thanks!!
Hey, I guess there are several ways of doing this. One way is :
//Load your text file TextReader tr = new StreamReader("\\test.txt"); //How many lines should be loaded? int NumberOfLines = 10; //Make our array for each line string[] ListLines = new string[NumberOfLines]; //Read the number of lines and put them in the array for (int i = 1; i = NumberOfLines; i++) { ListLines[i] = tr.ReadLine(); } //This will write the 5th line into the console Console.WriteLine(ListLines[5]); //This will write the 1st line into the console Console.WriteLine(ListLines[1]); Console.ReadLine(); // close the stream tr.Close();
Another way of doing it , could be :int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt"); while((line = file.ReadLine()) != null && counter < 10) { Console.WriteLine (line); counter++; } file.Close(); // Suspend the screen. Console.ReadLine();
You will have to play with it a little bit to get it right, but is not that hard. Hope this helps :-D -
Hey, I guess there are several ways of doing this. One way is :
//Load your text file TextReader tr = new StreamReader("\\test.txt"); //How many lines should be loaded? int NumberOfLines = 10; //Make our array for each line string[] ListLines = new string[NumberOfLines]; //Read the number of lines and put them in the array for (int i = 1; i = NumberOfLines; i++) { ListLines[i] = tr.ReadLine(); } //This will write the 5th line into the console Console.WriteLine(ListLines[5]); //This will write the 1st line into the console Console.WriteLine(ListLines[1]); Console.ReadLine(); // close the stream tr.Close();
Another way of doing it , could be :int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt"); while((line = file.ReadLine()) != null && counter < 10) { Console.WriteLine (line); counter++; } file.Close(); // Suspend the screen. Console.ReadLine();
You will have to play with it a little bit to get it right, but is not that hard. Hope this helps :-DThank you Rick but I still have a problem. I tryied out the second way you suggested, and it seems like it is counting the exact lines of the text file and not where "\par" is...I hope you understand what I'm trying to say... So I believe that I have to check where "\par" is in the stream and then increase the counter. Any suggestions??
-
Thank you Rick but I still have a problem. I tryied out the second way you suggested, and it seems like it is counting the exact lines of the text file and not where "\par" is...I hope you understand what I'm trying to say... So I believe that I have to check where "\par" is in the stream and then increase the counter. Any suggestions??
No problem .. :D Could you try to explain to me what "\Par" is .. is it a line break, or a text value that it is suppose to detect ?
-
Hello!! I have managed to load an rtf file in a streamreader : "StreamReader sr = new StreamReader("C:\\test.rtf");" and display it in a richtextbox: "richTextBox1.Rtf = sr.ReadLine();" Is there any way that I could posibly "read" each ,lets say, 10 lines? I suppose I could detect where this "\par" appears in the stream but don't know how to do it... Any suggestions??? Does the readline method have any operation for this? Thanks!!
Hi! There's nothing like this available out-of-the-box.
StreamReader.ReadLine()
reads until the next occurrance of \n, \r or \r\n, nothing to do about it. In an RTF document, these line break characters are more or less treated as white spaces, so there's no point in trying to useReadLine()
on an RTF file. You'd have to write your own method to read until\par
, but even then the RTF data will not be well-formed (for example, some closing braces will be missing) and I cannot say what a RichTextBox does with partially invalid data...Regards, mav -- Black holes are the places where God divided by 0...
-
No problem .. :D Could you try to explain to me what "\Par" is .. is it a line break, or a text value that it is suppose to detect ?
Hey rick, I don't want to cause any trouble to you... Anyway, "\par" is a linebreak like "
" in html. Rich Text Format uses the backslash to start it's control code instead of tags<>. So I have to detect where "\par" is in the document. I believe that this is the only way to read a formated document line by line. I hope you understand... Any ideas if it's no trouble??? Thanks!!!! -
Hi! There's nothing like this available out-of-the-box.
StreamReader.ReadLine()
reads until the next occurrance of \n, \r or \r\n, nothing to do about it. In an RTF document, these line break characters are more or less treated as white spaces, so there's no point in trying to useReadLine()
on an RTF file. You'd have to write your own method to read until\par
, but even then the RTF data will not be well-formed (for example, some closing braces will be missing) and I cannot say what a RichTextBox does with partially invalid data...Regards, mav -- Black holes are the places where God divided by 0...
Hey, thanks for the reply! I thought that readline isn't suitable and that I have to create a method for this. But don't know how... The method should be able to read from a stream in whitch the formated text of the rtf file is loaded and detect \par. For the problems you mentioned I believe I have to live with them... Could you help me out here??