954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help: Write 1-dimensional Array values to CSV

I'm a beginner in programming, and I'm doing a project that requires continously getting an input from the Serial Port. I already used the SerialPort class to retrieve the data and use it in my programm, and now I want to write those same SerialPort values (splitted into array[0]....array[7]) into .CSV
Please, I need help

Doughng
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

here is a small demo of what you can do:

string[] arrData = new string[5];
            arrData[0] = "This";
            arrData[1] = "is";
            arrData[2] = "a";
            arrData[3] = "Demo";
            arrData[4] = "Test";

            string tempString = "";
            foreach (string str in arrData)
            {
                tempString += str + ",";
            }

            tempString = tempString.Substring(0, tempString.Length - 1); //removing last ','

            System.IO.File.WriteAllText("my.csv", tempString);

Contents of my.csv files:

This,is,a,Demo,Test

i hope it helps

sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

I would rather suggest you to use generic list. But if you insist uisng string array, you can do it in this way too:

string[] array = new string[7];
//reading port and fill the array!
//then you can do:
string delimiter = ";";
//and write to a file:
string path = @"C\MyFolder\myFile.csv";
System.IO.File.WriteAllText(path, Stirng.Join(delimiter, array));
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

Thanks, what am actually working on collects inputs from the serial port, and I had to split the values to different arrays. So now, I am able to receive array[0], array[1]...
What I now need to do is to save the values of these arrays to a CSV.

Doughng
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Didnt we show you two example how yucan save the array into *.csv file?
Mine and and sandeepparehk0`s post have these kind of solutions.

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: