Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. rtf and streams

rtf and streams

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    panoskatws
    wrote on last edited by
    #1

    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!!

    R M 2 Replies Last reply
    0
    • P panoskatws

      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!!

      R Offline
      R Offline
      Rick van Woudenberg
      wrote on last edited by
      #2

      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

      P 1 Reply Last reply
      0
      • R Rick van Woudenberg

        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

        P Offline
        P Offline
        panoskatws
        wrote on last edited by
        #3

        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??

        R 1 Reply Last reply
        0
        • P panoskatws

          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??

          R Offline
          R Offline
          Rick van Woudenberg
          wrote on last edited by
          #4

          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 ?

          P 1 Reply Last reply
          0
          • P panoskatws

            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!!

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            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 use ReadLine() 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...

            P 1 Reply Last reply
            0
            • R Rick van Woudenberg

              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 ?

              P Offline
              P Offline
              panoskatws
              wrote on last edited by
              #6

              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!!!!

              1 Reply Last reply
              0
              • M mav northwind

                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 use ReadLine() 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...

                P Offline
                P Offline
                panoskatws
                wrote on last edited by
                #7

                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??

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups