473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Downloading unix \n text files, convert to \r\n non-unix?

I am downloading a file with \n newlines from a Unix system, and
storing it to a string. I want to convert it to \r\n newlines for
Windows. I know the StreamReader has an Encoding attribute, but this
isn't what I need. Should I do a String.Replace(), or is there a
better solution?

Zytan

Apr 5 '07 #1
13 4473
Should I do a String.Replace(), or is there a better solution?

String.Replace("\n", "\r\n") doesn't work. It doesn't change
anything.

Zytan

Apr 5 '07 #2
Zytan wrote:
>Should I do a String.Replace(), or is there a better solution?

String.Replace("\n", "\r\n") doesn't work. It doesn't change
anything.
It should.

string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = s.Replace("\n", "\r\n");
Console.WriteLine(s.Length);

prints 8 and 10 for me !

Arne
Apr 5 '07 #3
Arne Vajhøj wrote:
Zytan wrote:
>>Should I do a String.Replace(), or is there a better solution?

String.Replace("\n", "\r\n") doesn't work. It doesn't change
anything.

It should.

string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = s.Replace("\n", "\r\n");
Console.WriteLine(s.Length);

prints 8 and 10 for me !
But I think:

string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = Regex.Replace(s, "(?<!\r)\n", Environment.NewLine);
Console.WriteLine(s.Length);

is better code !

Arne
Apr 5 '07 #4
string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = s.Replace("\n", "\r\n");
Console.WriteLine(s.Length);

prints 8 and 10 for me !
LOL! I wasn't storing the value of s.Replace into s! And this after
I've confirmed your code works, and printed my string out as bytes, to
confirm that it is unix-style. haha

Zytan

Apr 5 '07 #5
But I think:
>
string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = Regex.Replace(s, "(?<!\r)\n", Environment.NewLine);
Console.WriteLine(s.Length);

is better code !
Thanks, Arne. So, this replaces both \r and \r\n with the proper
newline. So, this should work for any string! great!

Zytan

Apr 5 '07 #6
Zytan wrote:
>But I think:

string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = Regex.Replace(s, "(?<!\r)\n", Environment.NewLine);
Console.WriteLine(s.Length);

is better code !

Thanks, Arne. So, this replaces both \r and \r\n with the proper
newline. So, this should work for any string! great!
No.

It replace \n with \r\n without replacing \r\n with \r\r\n.

Arne
Apr 5 '07 #7
Zytan wrote:
>But I think:

string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = Regex.Replace(s, "(?<!\r)\n", Environment.NewLine);
Console.WriteLine(s.Length);

is better code !

Thanks, Arne. So, this replaces both \r and \r\n with the proper
newline. So, this should work for any string! great!
If you need to convert \n to \r\n and \r to \r\n
but keep existing \r\n untouched you should use:

s = Regex.Replace(s, "((?<!\r)\n)|(\r(?!\n))", "\r\n");

Note that I switched back from Environment.NewLine to \r\n
again - after thinking aboutn it, then Environment.NewLine is not
really good here.

Arne
Apr 6 '07 #8
On Apr 5, 7:36 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
Zytan wrote:
But I think:
string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = Regex.Replace(s, "(?<!\r)\n", Environment.NewLine);
Console.WriteLine(s.Length);
is better code !
Thanks, Arne. So, this replaces both \r and \r\n with the proper
newline. So, this should work for any string! great!

No.

It replace \n with \r\n without replacing \r\n with \r\r\n.
Well, \r\r\n wouldn't be proper, anyway, right, so it doesn't really
matter if you fix it or not, since it's already broken?

Your code ensures that only 0 or 1 \r is pre-fixed before \n, before
being replaced with Environment.NewLine, which is always the proper
carraige return for the system running the code. Right?

Zytan

Apr 6 '07 #9
If you need to convert \n to \r\n and \r to \r\n
but keep existing \r\n untouched you should use:
No, I just need to convert unix style (\n) into the current system
style (Environment.NewLine), although being able to convert either
style (\n or \r\n) into the current system style is good, since then I
don't care about which system I downloaded the text from. I think
your original code does this, fine.

Thanks, Arne

Zytan

Apr 6 '07 #10
On Fri, 06 Apr 2007 06:13:59 -0700, Zytan <zy**********@gmail.comwrote:
On Apr 5, 7:36 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
>Zytan wrote:
[...]
>It replace \n with \r\n without replacing \r\n with \r\r\n.

Well, \r\r\n wouldn't be proper, anyway, right, so it doesn't really
matter if you fix it or not, since it's already broken?

Your code ensures that only 0 or 1 \r is pre-fixed before \n, before
being replaced with Environment.NewLine, which is always the proper
carraige return for the system running the code. Right?
No. As he said, it replaces \n with \r\n without replacing \r\n with
\r\r\n. In ensures that exactly 0 \r is pre-fixed before \n before being
replaced, to phrase it in the same way you did.

You are right that \r\r\n would not be proper, which is why he wrote the
code to avoid creating instances of that.

Personally, I think it's overkill. If you're going to the trouble of
using the general purpose "Environment.NewLine" property as the replaced
text, then checking to avoid munging a specific character combination
(\r\n) seems pointless, given that in theory the point is that
"Environment.NewLine" might not actually be that specific character
combination. If nothing else, I would think it would make more sense to
design the code to replace *both* \n and \r\n with Environement.NewLine.

But ignoring that point, the code does do exactly what Arne says it does.

Pete
Apr 6 '07 #11
No. As he said, it replaces \n with \r\n without replacing \r\n with
\r\r\n. In ensures that exactly 0 \r is pre-fixed before \n before being
replaced, to phrase it in the same way you did.
Ah, ok. Regex is foreign to me.
You are right that \r\r\n would not be proper, which is why he wrote the
code to avoid creating instances of that.
Right.
I would think it would make more sense to
design the code to replace *both* \n and \r\n with Environement.NewLine.
Yes, that's what I was thinking Arne attempting to do (which is above
and beyond my original concern). And I agree this would be best.

Thanks, Pete.

Zytan

Apr 6 '07 #12
Zytan wrote:
On Apr 5, 7:36 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
>Zytan wrote:
>>>But I think:
string s = "A\nBB\nCCC";
Console.WriteLine(s.Length);
s = Regex.Replace(s, "(?<!\r)\n", Environment.NewLine);
Console.WriteLine(s.Length);
is better code !
Thanks, Arne. So, this replaces both \r and \r\n with the proper
newline. So, this should work for any string! great!
No.

It replace \n with \r\n without replacing \r\n with \r\r\n.

Well, \r\r\n wouldn't be proper, anyway, right, so it doesn't really
matter if you fix it or not, since it's already broken?
It is not about \r\r\n - it is about if the guys on the Unix
side suddenly decide to be nice and convert it to Windows
format \r\n for you. If you then run your program with the
simple replace you will get \r\r\n.

This variant avoids that, so the code can handle both
Unix and Windows format.

Arne
Apr 7 '07 #13
Peter Duniho wrote:
Personally, I think it's overkill. If you're going to the trouble of
using the general purpose "Environment.NewLine" property as the replaced
text, then checking to avoid munging a specific character combination
(\r\n) seems pointless, given that in theory the point is that
"Environment.NewLine" might not actually be that specific character
combination.
Which is one of the reasons why I dropped it in the last version.

Arne

Apr 7 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gary | last post by:
Hi, I have to convert some UNICODE text files to ANSI in VB 6. I thought this would be pretty straight forward, but the code blows up. I open the file as binary and then convert the whole mess...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
4
by: Matthew Crema | last post by:
Hello, Say I have 1000 text files and each is a list of 32768 integers. I have written a C program to read this data into a large matrix. I am using fopen in combination with fscanf to read...
2
by: Todd_M | last post by:
I was wondering what anyone might suggest as "best practice" patterns for streaming out fixed formatted text files with C#? Let's say we get our data in a dataset table and we need to iterate over...
8
by: Zephyre | last post by:
I have some UTF-8 text files written in Chinese to be read. Now the only method that I know to read text from it is to use fopen() function. Thus, I must read the contents byte by byte, change the...
5
by: praveenholal | last post by:
Hi Freinds , I want to convert the files that are in text format (.txt) to CSV file. I am working on Linux. So can anyone guide me. Here is my problem Description I have some result files...
5
by: Aneesh Pulukkul[MCSD.Net] | last post by:
How to convert a "Non Serializable" object to byte array. The object is a dynamically created Excel workbook. As per my understanding an object can be written and read from a stream Only if it's...
16
by: Wayne | last post by:
I've read that one method of repairing a misbehaving database is to save all database objects as text and then rebuild them from the text files. I've used the following code posted by Lyle...
8
by: jyaseen | last post by:
I used the follwing code to download the text file from my server location. my php file to read the following code name is contacts.php it is downloading the text file but , this text file...
8
by: James From Canb | last post by:
Is there a way to open very large text files in VBA? I wrote some code in VBA under Excel 2003 to read database extracts, add the field names as the first line, and to convert the fixed length...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.