0

I'm getting a string from an external native library function which uses "\n" for line separation. I simply want to write this to disk using the appropiate line ending format on the system I'm running my .Net application, which will be usually windows. So "Environment.NewLine" results correctly in "\r\n".

I'm simply write the string to a textfile via

MyString = NativeLib.GetStringEitherLForCRLF();
File.WriteAllText(MyFile, MyString); // will not change LF format

This doesn't changes lineendings, which seems to be intented that way. However, I'm wondering while the same string in C using fwrite in asciimode would probably change the lineendings as I remember.

Since conversion would be an easy task I'm just curios, how the "correct" or best-practice way in .Net should be. Is there a standard function, which would automatically convert the lineendings according to the current environment settings ?


I know how to replace characters in strings, that is not the basic question. The exact question is:

Does there exist a .Net standard library function that will do line ending conversions already depending on OS and Environment settings or which maybe has to be used with a certain parameter to ensure this?

Like e.g.:

File.WriteAllText(MyFile, MyString, Lineencoding.CRLF); // which does not exist

or

File.WriteAllText(MyFile, MyString.ConvertLineendings(Line.CRLF)); // which also does not

But there maybe an option I've missed but it doesn't look like that.
I do not want to write unnecessary boilerplate code to such a trivial task.

The problem is also described in Normalize newlines in C# just to show that I can be more complex than most people think. I'm not completely sure whether the proposed regex solution will work in 100% of all cases.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Florian Storck
  • 509
  • 8
  • 22
  • 1
    You can just do a string replacement. Have you tried that? – mason Feb 19 '18 at 15:27
  • @mason : hi mason, well, that would be easy but since it is that easy I'm just wondering if there exists any .Net standard function to handle this line ending stuff. And which I can simply use then by default to avoid problems in future. – Florian Storck Feb 19 '18 at 15:30
  • `input.Replace(@"\n", System.Environment.NewLine);` – Liam Feb 19 '18 at 15:31
  • @Liam: thanks for the hint. But I'm just looking for a Standard solution which is not errorprone to e.g. multiple replace calls. I thought there might be a library function which either handles this already internally instead of developing my own replacement solution, since this is a so common problem. – Florian Storck Feb 19 '18 at 15:35
  • You're making this out to be far harder than it is. All you have to do is replace one character with two characters within a string. Just write the code to do it. If you have specific problems, then create a [MCVE]. – mason Feb 19 '18 at 15:37
  • @mason: why ? it's at least a so ridiculous problem which still can cause problems if replacement is not checked well so I would assume that there maybe exist a function or option in the .Net library which will already do that. Otherwise, the simple answer would be : No, do a conversion on your own ! And e.g. fwrite in C does this if I remember correctly... – Florian Storck Feb 19 '18 at 15:45
  • 2
    What do you mean "checked well"? All it has to do is a simple string replacement. – mason Feb 19 '18 at 15:45
  • @mason: what, If Iget somehow a string containing "\r\n" and doing a "\n" replacement ? I have to first check the line endings format and then do my replacement. Otherwise I double lines. It's still simple, but there might be .Net setting which already does this automagically, since they wanted to be more portable. That's all I want to know. If not, then it's also okay but I simply want to know whether I'm not missing a function that already exists. – Florian Storck Feb 19 '18 at 15:49
  • and what makes you think [that](https://stackoverflow.com/questions/48869268/how-to-handle-line-endings-correctly-with-file-writealltext?noredirect=1#comment84742293_48869268) isn't standard and is error prone?! It's a very simple thing to do. – Liam Feb 19 '18 at 15:50
  • 1
    If you want to check for mixed line endings, then feel free to write some code to check for mixed line endings. – mason Feb 19 '18 at 15:50
  • I really do think you're over thinking this... – Liam Feb 19 '18 at 15:50
  • @Liam: well, of course it works. But not well if for any reason the string to convert has "\r\n" lineendings. And since this is an so old problem I would have assumed that .Net would already have some builtin solution, since it has a so huge library. Maybe I'm overthinking it but I just hate to write code were already a builtin solution exists. And I really don't know why asking for a builtin solution is being agressively downvoted... – Florian Storck Feb 19 '18 at 15:57
  • Why would .net have something built in for a single line of code?! In the time you've spent protesting that .net should do this, you could of solved this problem – Liam Feb 19 '18 at 16:01
  • also *[Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.](https://stackoverflow.com/help/on-topic)* – Liam Feb 19 '18 at 16:03
  • @Liam: because it's actually more then one line if I want to check the Input correctly as your link shows. And how many times big libraries have options that are hidden under a pile of other stuff ? And of course, I could have done the simpliest conversion by myself and built my own lib function and that would have been already done yet whithout dicussion. But actually is Stackoverflow not also about learning new things or functions you didn't know about yet? Why using own code if its already in the lib? I think, that is also a reason to ask even it looks rather trivial or simple. – Florian Storck Feb 19 '18 at 16:16
  • 1
    The problem is that what you are describing is application specific logic. Choosing what to do when there are mixed line endings, or no line endings, or all the line endings already match what your desired line endings are, these are all choices that you have to make yourself. The .NET Framework isn't going to expose such opinionated functionality for you. It's just going to give you the tools so that you can implement the logic yourself. – mason Feb 19 '18 at 16:23
  • @mason: thanks for your input. Well, as I've tried to explain I thought there might be a setting / function like in System.Globalization for handling Date / Time formats etc. In my simple understanding that would be not so uncommon, especially when you look at .Net core or whatever which could be also running on different OS...I mean, there are +12000 classes in the framework for all kinds of stuff, so I really wouldn't find this to uncommon if it would exist...but I'm quite suprised that this has been recepted so negatively. – Florian Storck Feb 19 '18 at 16:30
  • You missed the entire point of my previous comment. Yes, people have to handle line ending conversions all of the time, and the framework gives you ways to do that. But people have different requirements for how they need to process this data and perform these checks you've mentioned. There is no "one way" or "correct way" to do it. So the framework doesn't have a one stop `DoAllTheThings()` method for you. It's up to you to code your application logic according to your project's requirements. – mason Feb 19 '18 at 16:40
  • @mason: well, I don't want to annoy you, but I simply thought of an option like e.g. `File.WriteAllText(MyFile, MyInputString,Environment.OS.Newline)` to nail it to the OS specific format. Or whatever this option or conversion modifier would look like. I think, thats not completely uncommon. Of course I have to specify what I want and that is application logic. – Florian Storck Feb 19 '18 at 16:52
  • Again, you have missed the point of what is the framework's responsibility vs what is the app's responsibility. `File.WriteAllText` has a single purpose: to write a string to a file. It's job is not to manipulate the string. There are other methods for manipulating the string. It is your responsibility to decide what manipulations to perform and checks to do. The framework does not and should not attempt to write methods that do every possible thing you might want to do. Its job is to expose the necessary pieces for you, so that you can build your app according to your project's requirements. – mason Feb 19 '18 at 16:56
  • Furthermore, if you wanted to see what overloads of `File.WriteAllText` are available, you can easily [check the documentation](https://msdn.microsoft.com/en-us/library/ms143375(v=vs.110).aspx). There isn't any need to ask about it on Stack Overflow. – mason Feb 19 '18 at 16:58
  • @mason: yes, of course, I just wanted that as an example. I experienced a few times that I've overseen an option because of the number of overloads and other stuff so I didn't wanted it to nail it down to that specific function. I would expect it in System.Text or whereever, but if it doesn't exist a native function its also okay. Also as I already stated, the answer "no, the framework doesn't support it please do on your own" would be perfectly ok. But there are a lot of "trivial" tasks more seldomly used which have an own .Net library function. – Florian Storck Feb 19 '18 at 17:07

1 Answers1

2

If you really get the input as a string (as opposed to a file or something), then create a StringReader over it. On the reader, do .ReadLine() in a loop and write each line to your file (e.g. with a StreamWriter).

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • Except StreamWriter.WriteLine will always use `\r\n`, even on linux (as far as I know). – Evk Feb 19 '18 at 16:08