StreamWriter writes all string on one line instead of appending each string on a new line

Andreas ss 726 Reputation points
2022-03-22T00:31:42.987+00:00

Hello!

I noticed a strange problem. The below code whould write 4 lines in a notepad file.

When I manually open the testfile.txt in the notepad application, it looks like it is 4 lines. But when looking in notepad, all those 4 lines exists on 1 line in notepad which is strange.
I notice that it is because of the last line I write in the file which is a very long string as can be seen below. If removing the last line in this code, then 3 lines will be seen in the notepad application.

I don't understand why the last line which is very long makes everything exists on 1 line?
I have those long strings and need each string to be appended on a new line in notepad.

What am I doing wrong? /Thank you

            StreamWriter writer = null; FileStream fs = null;
            fs = new FileStream("C:/folder1/testfile.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite); writer = new StreamWriter(fs);
            writer.WriteLine("ooo");
            writer.WriteLine("ooo");
            writer.WriteLine("ooo");
            writer.WriteLine("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
            writer.Close(); fs.Close();
            
Windows for business | Windows Server | User experience | Other
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2022-03-24T01:24:20.703+00:00

    Your problem appears to be with the version of Notepad you're using,
    or with the underlying Windows control that Notepad is using.

    The auto wrapping of long lines is occurring after 1024 characters.
    That is a significant number within the binary number system. It is
    two to the power of 10. It may signify a limitation on the size
    and therefore capacity of an internal variable.

    As for others, I do not see that anomaly in Win 10 21H2

    Examine the file created using a different editor or file viewer
    to confirm that it is a Notepad issue, and that the file itself
    is formatted correctly.

    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.