James Archer James Archer - 2 months ago 25
C# Question

Saving a double array to a text file c#

I have an array of floating point numbers which has an unknown length as the the array is dynamic (it grows as new data is added to it). I need to find a way to store the data into a text file.

I would like the text file to be formatted in such a way that there is one floating point number per line.

I've been searching the web but could not find a solution, I'm new to c# so I would be very grateful if someone could point me in the right direction to solving the problem.

Answer

You can do it in a single line in .NET 4.0, like this:

File.WriteAllLines(
    @"c:\data\myfile.txt" // <<== Put the file name here
,   myDoubles.Select(d => d.ToString()).ToArray()
);
Comments