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.

Henkealg
  • 1,466
  • 1
  • 16
  • 19

4 Answers4

5
string outDataString = string.Format(
"Hi, this is the out data.{0}With a few{0} line breaks.",
Environment.NewLine);
Alireza
  • 10,237
  • 6
  • 43
  • 59
  • 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 – Panagiotis Kanavos Dec 05 '13 at 10:18
  • 2
    @Panagiotis It's more readable and has no that much performance issue to worry – Alireza Dec 05 '13 at 10:20
  • @PanagiotisKanavos In this case, the Format operation is being used to ensure that the code works cross-platform. Although on Windows you want to use \r\n, if you were running on Linux you'd only want \n. Using Environment.NewLine ensures that the correct characters are being used regardless of what platform you're running on. – Chris Dec 05 '13 at 10:21
  • Unless you run it into a frequent loop (eg. polling a service or writing multiple lines), while looking at a memory profiler ramping up from a few MBs to tens of MBs. Depends on what you are doing – Panagiotis Kanavos Dec 05 '13 at 10:21
  • 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 Dec 05 '13 at 10:23
  • @Alireza In this case the application will run in a Windows environment so I will go for the \r\n suggestion. It seems to give least overheads in terms of performance. Your answer is great though, thanks! – Henkealg Dec 05 '13 at 10:25
  • 1
    @Henkealg Think about readability. Not only '\r\n' isn't readable, but also it is vulnerable to typo error. – Alireza Dec 05 '13 at 10:27
  • 1
    @PanagiotisKanavos John Skeet says there is nothing to worry about: http://stackoverflow.com/questions/296978/when-is-it-better-to-use-string-format-vs-string-concatenation – Alireza Dec 05 '13 at 10:27
  • Totally different question actually, he's talking about two ways of constructing a string, not comparing a static string with a dynamically constructed one. Besides, WriteLine is even simpler – Panagiotis Kanavos Dec 05 '13 at 10:39
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);
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
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);
Seany84
  • 5,526
  • 5
  • 42
  • 67
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.

Chris
  • 4,661
  • 1
  • 23
  • 25