0

I am trying to write an array to a file with C# and am having issues. I've started learning c# the last few days and now can't figure out why this happens.

    namespace Arrays
{
    class Program
    {
        static void Sort()
        {

        }

        public static void Random()
        {
            int[] test2 = new int[5];
            int Min = 1;
            int Max = 100;

            Random randNum = new Random();
            for (int i = 0; i < test2.Length; i++)
            {
                test2[i] = randNum.Next(Min, Max);
                Console.WriteLine(test2[i]);
            }


        Console.WriteLine("");
        for (int ii = 0; ii < test2.Length; ii++)
        {
            Array.Sort(test2);
            Console.WriteLine(test2[ii]);
        }


            String writeToText = string.Format("{0}", test2);
            System.IO.File.WriteAllText(@"C:\\Users\\hughesa3\\Desktop\\log.txt", writeToText); // Writes string to text file

        }
        static void Main(string[] args)
        {
            Random();
        }
        }
    }

It generates a random 5 numbers and puts it into the array, When I try to write this to a file it prints System.Int32[]

I understand that because im trying to print a formated string but how would i go about printing each int ? I've tried using a loop but it will only save the last int as i put it inside the loop?

Can anyone give me some advice ?

Thanks

3 Answers 3

4

Use WriteAllLines and pass string array as input.

System.IO.File.WriteAllLines("filename", test2.Select(i=>i.ToString()).ToArray());

or, if you want to write in , separated form use this.

System.IO.File.WriteAllText("filename", string.Join(",", test2.Select(i=>i.ToString()).ToArray());
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks that works for me, I am trying to write the numbers unsorted first and then below that write them sorted, When I run it now the sorted one will overwrite the unsorted one?
if you want to sort Just use OrderBy Linq extension like test2>OrderBy(t=>t).Select(i=>i.ToString()).ToArray().
Array.Sort(test2); works perfectly for this its just when i try to save the sorted array to a file it overwrites the unsorted values
Use File.AppendAllLines instead of WriteAllLines.
Also AppendAllText instead of WriteAllText.
1

The problem is that String writeToText = string.Format("{0}", test2); calls ToString method of the test2 array and it returns System.Int32[]

Change it to

String writeToText = string.Join("", test2.Select(x=>x.ToString())

or

String writeToText = string.Format("{0}", test2.Select(x=>x.ToString().Aggregate((c,n)=>string.Format("{0}{1}", c,n))

Comments

0
    //Add this method and use in System.IO.File.WriteAllText(@"C:\\Users\\hughesa3\\Desktop\\log.txt", ArrayToString(test2));
    public static String ArrayToString(int[] arr)
    {
        return arr.Aggregate("", (current, num) => current + (num + " "));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.