2

I am having problems handling line breaks in the out data for a console application I an working on. The simplfied example below is writing outDataString to out.txt without line breaks. Any thoughts on how to achieve line breaks in out.txt in this case?

FileStream ostrm;
StreamWriter writer;

string outDataString = "Hi, this is the out data.\nWith a few\n line breaks.";

ostrm = new FileStream("out.txt", FileMode.OpenOrCreate, FileAccess.Write);                
writer = new StreamWriter(outDataString);

Console.SetOut(writer);
Console.write(outDataString);

Thanks.

3
  • Does this code compile? Commented Dec 5, 2013 at 10:30
  • The example is not complete. It is just to display my question.
    – Henkealg
    Commented Dec 5, 2013 at 10:32
  • Use Console.WriteLine for each line then and you won't have any issues. Even if you want to use a static literal, you can define an array of lines, eg var lines=new[] {"Line1","Line2","Line3"} Commented Dec 5, 2013 at 10:37

4 Answers 4

5
string outDataString = string.Format(
"Hi, this is the out data.{0}With a few{0} line breaks.",
Environment.NewLine);
9
  • No reason to execute a Format operation when you can create a valid string constant. The string literal will be interned while the Format operation will force the repeated creation and disposal of unnecessary objects Commented Dec 5, 2013 at 10:18
  • 2
    @Panagiotis It's more readable and has no that much performance issue to worry
    – Alireza
    Commented Dec 5, 2013 at 10:20
  • 4
    @PanagiotisKanavos This is premature optimization. Start with what is most correct, which is the use of Environment.NewLine, then if (if!) there are performance issues you can look into alternatives.
    – Chris
    Commented Dec 5, 2013 at 10:23
  • 1
    @Henkealg Think about readability. Not only '\r\n' isn't readable, but also it is vulnerable to typo error.
    – Alireza
    Commented Dec 5, 2013 at 10:27
  • 1
    @PanagiotisKanavos John Skeet says there is nothing to worry about: stackoverflow.com/questions/296978/…
    – Alireza
    Commented Dec 5, 2013 at 10:27
1
FileStream ostrm;
StreamWriter writer;

string outDataString = String.Format("Hi, this is the out data.{0}With a few{0} line breaks.", System.Environment.NewLine);

ostrm = new FileStream("out.txt", FileMode.OpenOrCreate, FileAccess.Write);                
writer = new StreamWriter(outDataString);

Console.SetOut(writer);
1

I think the original code should be:

var ostrm = new FileStream("out.txt", FileMode.OpenOrCreate, FileAccess.Write);                
var writer = new StreamWriter(ostrm);

Console.SetOut(writer);

Console.Write(outDataString);

In Windows and DOS before it, you need to enter \r\n to change the line. Actually, this goes back to CP/M.

You can just write :

string outDataString = "Hi, this is the out data.\r\nWith a few\r\n line breaks.";

If you don't really want to write a string literal containing multiple lines, it's better to use StreamWriter.WriteLine to write each line separately, eg:

Console.WriteLine(firstLine);
Console.WriteLine(secondLine);

or even

var lines=new[]{"My First Line","My Second Line"};
foreach(var line in lines)
    Console.WriteLine(secondLine);
4
  • 1
    Using System.Environment.Newline makes newlines portable to other platforms (e.g. Mono on *NIX). Commented Dec 5, 2013 at 10:18
  • And forces an unnecessary operation with memory and CPU costs, which is why the use of Environment.NewLine is so limited Commented Dec 5, 2013 at 10:20
  • 2
    Portability trumps micro-optimizations in my book, but YMMV :) Commented Dec 5, 2013 at 10:21
  • There is a thing like micro-portability too. Besides, you can write a platform agnostic newline to the console without actually forcing a format Commented Dec 5, 2013 at 10:35
0

What platform is this running on? On Windows, you need a carriage return AND a newline to represent a line break, so you need to replace \n with \r\n

Ideally, rather than embedding newlines in literals in this fashion, you should use Environment.NewLine instead, which would make your code more likely to work cross-platform.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.