Welcome to Dream.In.Code
Become a C# Expert!

Join 150,413 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,019 people online right now. Registration is fast and FREE... Join Now!




txt->csv File Conversion

 
Reply to this topicStart new topic

txt->csv File Conversion

davegeek
25 Mar, 2008 - 02:39 PM
Post #1

D.I.C Head
Group Icon

Joined: 30 Jan, 2008
Posts: 81



Thanked: 2 times
My Contributions
Hi there.

My original idea was to convert the text file into csv putting the comma among all the txt file elements.
Could someone tell me what I am doing wrong? Thing is that I get the error message attached.
When I am trying to convert, and the "\r\n" occurs (carriage return) so a new comma is put into the csv file what I dont wanna happen.
So, I put the if-else condition that says if the \r\n occurs than skip else continue with the next element.

CODE

            TextReader reader = new StreamReader("c:/FileLogs/Log.txt");
            string a = reader.ReadToEnd();
            char[] array = a.ToCharArray();
            reader.Close();
            FileStream aFile = new FileStream("c:/FileLogs/Log.csv", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(aFile);
            for(int i=0; i<array.Length; i++)
            {
                if (array[i].ToChar = "\n\r")
                    sw.WriteLine();
                else
                sw.Write(a[i] + ",");
            }
            sw.Close();


Any ideas how to fix it?

This post has been edited by davegeek: 25 Mar, 2008 - 02:42 PM


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

marcells23
RE: Txt->csv File Conversion
25 Mar, 2008 - 03:23 PM
Post #2

D.I.C Head
Group Icon

Joined: 22 Aug, 2007
Posts: 139



Thanked: 3 times
Dream Kudos: 125
My Contributions
try using array[i].ToChar == "\n\r"
User is offlineProfile CardPM
+Quote Post

orcasquall
RE: Txt->csv File Conversion
26 Mar, 2008 - 05:10 AM
Post #3

D.I.C Head
Group Icon

Joined: 14 Sep, 2007
Posts: 158



Thanked: 3 times
Dream Kudos: 50
My Contributions
Might also want to try this
csharp

if (array[i] == '\r' && array[i+1] == '\n')

The && in C# is a short-circuit, so you'll not hit an array index out of bounds with i+1 if array[i] is not an '\r'. Maybe... tongue.gif
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Txt->csv File Conversion
26 Mar, 2008 - 07:06 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Actually in this situation you want to use the OR (||) symbol and check for either one of the escape sequences.

CODE

if (array[i].ToChar == "\n" || array[i].ToChar == "\r")
     sw.WriteLine();
else
     sw.Write(a[i] + ",");

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Txt->csv File Conversion
26 Mar, 2008 - 09:31 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,291



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
I kind of don't get this one. Do you seriously want to turn this:
CODE

This is
a test

Into this:
CODE

T,h,i,s, ,i,s
a, ,t,e,s,t

?

And you want to turn a "\n\r" into "\n"? This simplest for the latter is just to strip the "\r", since you're left with a newline.

Here's the code I'd use to do what you seem to be trying to do:
csharp

void CommaMe(string s, TextWriter writer) {
Regex re = new Regex("(.)");
foreach (string line in s.Replace("\r", "").Split('\n')) {
// don't want a "," in front of the first char, print is separate
writer.Write(line[0]);
writer.WriteLine(re.Replace(line.Substring(1), ",$1"));
}
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

davegeek
RE: Txt->csv File Conversion
26 Mar, 2008 - 01:15 PM
Post #6

D.I.C Head
Group Icon

Joined: 30 Jan, 2008
Posts: 81



Thanked: 2 times
My Contributions
Guys.

From all the replies posted here I find useful the only one - by Mr. baavgai.
But the thing is that u are using some sort of regular expression with the split method.
Can we do it simply by using some less complex code like the one that i wrote initially?

by the way: the answer tou your question is yes, I wanna convert "this is a test" into "t,h,i,s...."

This post has been edited by davegeek: 26 Mar, 2008 - 01:17 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Txt->csv File Conversion
26 Mar, 2008 - 02:36 PM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,291



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(davegeek @ 26 Mar, 2008 - 05:15 PM) *

But the thing is that u are using some sort of regular expression with the split method.
Can we do it simply by using some less complex code like the one that i wrote initially?


But, but, I like regular expressions... wink2.gif

You're basically there, actually. The real trick lies in the [li]s.Replace("\r", "")[/li]. By just getting rid of the offending character, the code get's very simple.

By splitting out the lines, you just have to process each line by itself, with a writeline at the end.

Adjusted code would look like so:
csharp

void CommaMe(string s, TextWriter writer) {
foreach (string line in s.Replace("\r", "").Split('\n')) {
for(int i=0; i<line.Length; i++) {
if (i>0) { writer.Write(","); }
writer.Write(line[i]);
}
writer.WriteLine();
}
}


With a call like so:
csharp

TextReader reader = new StreamReader("c:/FileLogs/Log.txt");
string a = reader.ReadToEnd();
reader.Close();

FileStream aFile = new FileStream("c:/FileLogs/Log.csv", FileMode.OpenOrCreate);
CommaMe(a, new StreamWriter(aFile));
aFile.Close();


User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month