How to read/write data in UNIX Format (.NET)

Custom / CUSTOM
September 7, 2009 at 21:02:13
Specs: Microsoft Windows XP Professional, 2.8 GHz / 2039 MB
I want to ask how to read or write data in UNIX format.
What's their differences?

I need to read a UNIX format input file then I need to insert some phrases inside the file.

Help is much apreciated.

See More: How to read/write data in UNIX Format (.NET)

Report •

September 8, 2009 at 05:28:16
I'm asuming that you are reading an ASCII (text) UNIX file. The difference between UNIX and DOS text files is the line ending. UNIX has only a CR (carriage return) while DOS has a CRLF (carriage return line feed) pair. Windows-DOS software will write the CRLF pair at the end every of line. I don't know how much editing you have. There use to be text editors with a binary mode that will rewrite the file correctly. Or you can can write your own editor/converter.

Report •

September 8, 2009 at 05:46:32
By "Unix file" most people typically mean a plain ASCII text file where each line is terminated by an LF character (ASCII 10, or '\n') instead of the Windows CR+LF pair ("\r\n").

The StreamWriter.WriteLine method will write Environment.NewLine at the end. Environment.NewLine is defined as "\r\n" on Windows and "\n" on Unix.

The StreamReader.ReadLine method will read one line that is terminated by either "\n" or "\r" or the pair "\r\n". So if you are reading a Unix text file on Windows, it's all done seamlessly.

Report •

September 9, 2009 at 19:45:17
thank you, so the difference is the end of the lines then.

Report •

Related Solutions

September 10, 2009 at 01:38:41
You also asked about writing in Unix format. That can be a bit trickier, because if you use the WriteLine method on .NET on Windows it will write CR-LF. One easy way around it is to write the file in Windows format and then use the Unix utility dos2unix (sometimes called d2u) to convert it (this utility replaces CR-LF character pairs with a single LF character.

Report •

Ask Question