Iam having trouble writing my results to a CSV file.The results i have are as follows

-0.025 0.15 0.15 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5

When i am writing them to the file it just prints them in one low row.

I want the program to take a new row after 5 results:

-0.025 0.15 0.15 0.4 0.5
-0.05 0.1333 0.2 0.4 0.5

and so on.my code so far is as folows:

foreach (KeyValuePair<int, float> _value in dic1)
                {
                    averageLine1.Add(_value.Value);


                    string Result = Convert.ToString(_value.Value);

                    // Create a new file called 'TraceAverages.csv'and write the values to it:
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\40025634\Desktop\Project\List1Averages.csv", true))
                    {
                        file.Write("{0},", Result);

                    }
                }
            
            averageLine1.Clear();
        }

ANY IDEAS?

Use a counter and write a newline after every 5 counts.

Use a counter and write a newline after every 5 counts.

Yeh i tried using a counter but everytime i did it wouldnt work. il try again.

Something like this?

// Create a new file called 'TraceAverages.csv'and write the values to it: 
            int counter = 0;       
            using 
           (
                System.IO.StreamWriter file = 
                new System.IO.StreamWriter(@"C:\Users\40025634\Desktop\Project\List1Averages.csv", true))

                {
                    file.Write("{0},", "aresult"); 
                    counter++;
                    if (counter > 5)
                    {
                        file.WriteLine();
                        counter = 0;
                    }
            }

Something like this?

// Create a new file called 'TraceAverages.csv'and write the values to it: 
            int counter = 0;       
            using 
           (
                System.IO.StreamWriter file = 
                new System.IO.StreamWriter(@"C:\Users\40025634\Desktop\Project\List1Averages.csv", true))

                {
                    file.Write("{0},", "aresult"); 
                    counter++;
                    if (counter > 5)
                    {
                        file.WriteLine();
                        counter = 0;
                    }
            }

I STILL CANT GET IT WORKING FOR THIS BIT!!!! ANY IDEAS????? THIS IS MY CODE:

{
                for (int j = 0; j < 5; j++)
                {
                    for (int i = 0; i < 6; i++)
                    {
                        String position128 = SboxArray[i, j].Substring(0, 1);

                        if (position128 == "0")
                            list1.Add(tracearray[i]);
                        else
                            list2.Add(tracearray[i]);
                    }



                    FindaveragesBinary0(list1, averageLine1);

                    FinaveragesBinary1(list2, averageLine2);



                    //counts the number of values inaverageSbox and stores it in the variable i:
                    for (int i = 0; i < averageLine1.Count; i++)
                    {

                        //counts the number of values averageTrace and stores it in the variable j:
                        for (int x = 0; x < averageLine2.Count; x++)
                        {

                            // when j and i equal each other:
                            int counter = 0;
                            if (x == i)
                            {

                                // take the two values at index i and index j and substract them:
                                float _newValue = averageLine1[i] - averageLine2[x];

                                // add the new value to a list 'ResultList':
                                // Create a new file called 'Results' and write the values to it:

                                ResultList.Add(_newValue);

                                {
                                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\40025634\Desktop\Project\Results.csv", true))
                                        {
                                            
                                           file.Write("{0},", _newValue); 
                                            counter++;
                                            if (counter > 5)
                                            {
                                                file.WriteLine();
                                                counter = 0;
                                            }
                                        }
                                    
                                }
                                ResultList.Clear();

                            }
                        }
                    }
                    averageLine1.Clear();
                    averageLine2.Clear();
                }


                Console.Write("ok");
                Console.ReadKey();
            }

PLEASE HELP??

Try this code, it works 100%:

private void WriteToFile()
        {
            string line = "-0.025 0.15 0.15 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5";
            string path = @"C:\1\test27.txt";
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
            {
                using (TextWriter tw = new StreamWriter(fs))
                {
                    string[] array = line.Split(' ');
                    string textToWrite = null;
                    for (int i = 1; i <= array.Length; i++)
                    {
                        if (i % 5 == 0 && i > 1)
                        {
                            textToWrite += array[i - 1];
                            tw.WriteLine(textToWrite);
                            textToWrite = null;
                        }
                        else
                            textToWrite += array[i - 1] + " ";
                    }
                }
            }
        }

Try this code, it works 100%:

private void WriteToFile()
        {
            string line = "-0.025 0.15 0.15 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5 -0.05 0.1333333 0.2 0.4 0.5";
            string path = @"C:\1\test27.txt";
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
            {
                using (TextWriter tw = new StreamWriter(fs))
                {
                    string[] array = line.Split(' ');
                    string textToWrite = null;
                    for (int i = 1; i <= array.Length; i++)
                    {
                        if (i % 5 == 0 && i > 1)
                        {
                            textToWrite += array[i - 1];
                            tw.WriteLine(textToWrite);
                            textToWrite = null;
                        }
                        else
                            textToWrite += array[i - 1] + " ";
                    }
                }
            }
        }

I have tried that way.i cant get it working cause i dont really understand it. sorry.

mmm i tried this . . but any ideas why it wont work?

ResultList.Add(_newValue);

                                    foreach (float A in ResultList)
                                    {
                                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Mark\Desktop\University\Final Year Project\Results.csv", true))
                                        {

                                            if (counter1 == 5)
                                            {
                                                file.Write("{0},", _newValue);
                                                file.WriteLine();
                                                counter1 = 0;
                                            }
                                            else
                                                file.Write("{0},", _newValue);
                                            counter1++;
                                        }
                                    }
                                    ResultList.Clear();

                                }

Mark522 modify your code like this

private void WriteToFile()
            {
               
                string path = @"C:\Users\Mark\Desktop\University\Final Year Project\Results.csv"
                string textToWrite = null;
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                {
                    using (TextWriter tw = new StreamWriter(fs))
                {
                      foreach (float A in result)
                                    {
                                        if (counter1 == 5)
                                        {

                                            counter1 = 1;
                                            tw.WriteLine();
                                        }
                                        else
                                        {
                                            counter1++;
                                        }
                          textToWrite=A+" ";
                          tw.Write(textToWrite);
                          textToWrite = null;
                         
                                        }
                    }
                }
                           
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.