Results 1 to 3 of 3

Thread: Text all being displayed in one line in notepad

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2015
    Posts
    6

    Text all being displayed in one line in notepad

    I am having an issue where when I write text to a second file, it display fine in wordpad where it includes the linebreaks, but in notepad all the text for copyText and richTextBox1.Text appear in one really long line. How can I fix this? If somebody can include a code snippet with my code then it will be much appreciated as I can see what you have done and changed and I can keep it for reference for futr use.

    My code is below where I take text from a text file (Testfile.txt) and insert it into rich text box 1, then when I click on button, text is copied to rich text box 2 and is written in second file (_Parsed.txt). I did a message box on text line and it displays text all without line break. I'm not sure if that is issue but I do need help as deadline is tomorrow and this is only thing I need to sort out and I'm done.

    Code:
    string Chosen_File = "C:\\_Testfile.txt";
    string Second_File = "C:\\_Parsed.txt";
    string wholeText = "";
    
    private void mnuOpen_Click(object sender, EventArgs e) {
    
            //Add data from text file to rich text box
            richTextBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);
    
            //Read lines of text in text file                
            string textLine = "";
    
            StreamReader txtReader;
            txtReader = new StreamReader(Chosen_File);
    
            do {
                textLine = textLine + txtReader.ReadLine() + " ";
            }
    
            //Read line until there is no more characters
            while (txtReader.Peek() != -1);
    
            richTextBox1.Text = textLine;
    
            txtReader.Close();
        }
    
    }
    
    private void Write(string file, string text) {
    
        //Check to see if _Parsed File exists
    
            //Write to _Parsed text file
            using(StreamWriter objWriter = new StreamWriter(file)) {
                objWriter.Write(text);
                objWriter.Close();
        } 
    
    }
    
    private void newSummaryMethod(string copyText) {
    
        //Write into richTextBox2 all relevant text
        copyText = richTextBox1.Text;
        wholeText = richTextBox1.Text + copyText
        Write(Second_File, wholeText);
    }
    
    
    private void btn1_Click(object sender, EventArgs e) {
    
            newSummaryMethod(copyText);
    }

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Text all being displayed in one line in notepad

    I'm a little surprised it displays correctly in Wordpad.
    I expected the reason it displays fine in Wordpad, but not in Notepad, would be due to the line Termination characters.
    Notepad expects each line to be terminated by a two character sequence, Cr (13 decimal ASCII) and Lf (10 decimal ASCII).
    If the line doesn't end with CrLf notepad doesn't start a new line.

    You usually see this issue if you look at text files from Linux or Macs on a Windows machine.
    I don't know if it is still true for Macs, but Macs you to end their lines with just a single Cr.
    Linux machines tend to end their lines in text files with a single Lf.

    WordPad understands those three variations so will treat a single Lf, or a single Cr, or a combined CrLf as a single end of line indication.

    The line
    textLine = textLine + txtReader.ReadLine() + " ";
    you have in your code I would have expected to removed the Line Termination character(s) (by the ReadLine function) and replace it/them with " ", a space character.
    I'm not much of a C# person so won't attempt an example, but perhaps replace the " " at the end of that line with a CrLf sequence.
    {My mouse is acting up, continually moving in one direction, so I can't really test anything conveniently, so will have to post this and restart my computer to get the mouse problem fixed}.
    There may be a system constant in C# that you can use that represent the newLine char(s) that you can use instead of the " ".

    You might want to look at the raw bytes of the file to see if one of the characters is there, as it appears one of them may be, otherwise how would wordpad know where to linebreak.
    If one was present, perhaps you would only have to add the missing one.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2015
    Posts
    6

    Re: Text all being displayed in one line in notepad

    Quote Originally Posted by passel View Post
    I'm a little surprised it displays correctly in Wordpad.
    I expected the reason it displays fine in Wordpad, but not in Notepad, would be due to the line Termination characters.
    Notepad expects each line to be terminated by a two character sequence, Cr (13 decimal ASCII) and Lf (10 decimal ASCII).
    If the line doesn't end with CrLf notepad doesn't start a new line.

    You usually see this issue if you look at text files from Linux or Macs on a Windows machine.
    I don't know if it is still true for Macs, but Macs you to end their lines with just a single Cr.
    Linux machines tend to end their lines in text files with a single Lf.

    WordPad understands those three variations so will treat a single Lf, or a single Cr, or a combined CrLf as a single end of line indication.

    The line
    textLine = textLine + txtReader.ReadLine() + " ";
    you have in your code I would have expected to removed the Line Termination character(s) (by the ReadLine function) and replace it/them with " ", a space character.
    I'm not much of a C# person so won't attempt an example, but perhaps replace the " " at the end of that line with a CrLf sequence.
    {My mouse is acting up, continually moving in one direction, so I can't really test anything conveniently, so will have to post this and restart my computer to get the mouse problem fixed}.
    There may be a system constant in C# that you can use that represent the newLine char(s) that you can use instead of the " ".

    You might want to look at the raw bytes of the file to see if one of the characters is there, as it appears one of them may be, otherwise how would wordpad know where to linebreak.
    If one was present, perhaps you would only have to add the missing one.
    My apologies, it's because word wrap in notepad wasn't enabled. Thank you for this useful info for future reference though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width