John Jacothan
John Jacothan

Reputation: 23

Adding text to a file is displayed in one long line

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 richTextBox1, then when I click on button, text is copied to richText Box2 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.

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);
}

Upvotes: 2

Views: 60

Answers (2)

Denis  Yarkovoy
Denis Yarkovoy

Reputation: 1307

The problem is probably with the line breaks in your text file(s). TextBoxes expect \r\n sequence as an indication of the end-of-line, and your text files may contain only the \n. Try to make this change to your code:

 do {
        textLine = textLine + txtReader.ReadLine() + "\r\n";
    }

    //Read line until there is no more characters
    while (txtReader.Peek() != -1);

Upvotes: 1

Hamid Pourjam
Hamid Pourjam

Reputation: 20754

The problem is that you are never adding a new line to the file in your code. you are using objWriter.Write(text); which does not generate a new line.

you can use objWriter.WriteLine(text); instead and it will automatically add new line character at the end.

I think the difference in viewing the file is because the "word wrap" option is turned on in "wordpad" and it causes to break a long line into multiple line.

Upvotes: 0

Related Questions