none
File.ReadAllLines problems RRS feed

  • Question

  • Hello everybody

    I wrote a simple code which allows you to save your grades but i can't get to read them and save all the previous inputs. I don't know how to use the File.ReadAllLines properly so it reads the data and saves it and later i can't add new grades. Now whenever i start the program and put new grades the old ones disapear.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static int ReadGrade()
            {
                int x;
                try
                {
                    x = Convert.ToInt32(Console.ReadLine());
                    if (x < 1 || x > 6)
                    {
                        throw new ApplicationException("'Invalid input!'");
                    }
                }
    
                catch (Exception e)
                {
                    Console.WriteLine("Error " + e.Message);
                    x = 0;
                }
                return x;
            }
    
            static float mtlGrade(float[] x)
            {
                float mw = x[0];
                for (int i = 1; i < x.Length; i++) mw += x[i];
                return mw / x.Length;
            }
    
            static void Main(string[] args)
            {
                int amRest = 3; // amount rest
                int amTes = 4; // amout test
                float[] help = new float[amTes];
    
                //subjects
                string[] subjects = {"German", "English", "Math", "ITS", "SA", "Sport", "Religion", 
    						       "Politik", "WL", "Electrotechnik"};
                int[,] test = new int[subjects.Length, amTes];
                int[,] rest = new int[subjects.Length, amRest];
    
                string path = @"c:\temp";
                //read data
                if (File.Exists(path + "\\" + "MatFlo.csv"))
                {
                    string[] readText = File.ReadAllLines(path + "\\" + "MatFlo.csv", Encoding.UTF8);
                    string[] row;
                    //save all data in "s"
                    foreach (string s in readText)
                    {
                        //save data
                        for (int i = 0; i < subjects.Length; i++)
                        {
                            row = s.Split(';');
                            subjects[i] = row[0];
                            for (int j = 0; j < amTes; j++) test[i, j] = Convert.ToInt32(row[j + 1]);
                            for (int k = 4; k < amRest; k++) rest[i, k] = Convert.ToInt32(row[k + 1]);
    
                            Console.WriteLine(s);
    
                        }
                    }
                }
                Console.Clear();
                
                //list subjects
                for (int i = 0; i < 10; i++)
                {
                    if (i < 9)
                    {
                        Console.WriteLine((i + 1) + "  :" + subjects[i]);
                    }
                    else
                    {
                        Console.WriteLine((i + 1) + " :" + subjects[i]);
                    }
                }
                //choose subject
                int subject; //number of the subject
                subject = Convert.ToInt32(Console.ReadLine()); subject--;
                Console.Clear();
                Console.WriteLine("Du hast das subject " + subjects[subject] + " gewählt!" + "\n");
    
                //input test
                for (int i = 0; i < amTes; i++)
                {
                    Console.Write((i + 1) + " Klausur Note :");
                    test[subject, i] = ReadGrade();
                    help[i] = test[subject, i];
                }
                Console.WriteLine("\n");
    
                //input rest
                for (int i = 0; i < amRest; i++)
                {
                    Console.Write((i + 1) + " restige Note :");
                    rest[subject, i] = ReadGrade();
                    help[i] = test[subject, i];
                }
    
    
                float avarageTes = 0;
                float avarageRest = 0;
                //avarage of Test
                for (int i = 0; i < amTes; i++)
                {
                    avarageTes += test[subject, i];
                }
                avarageTes /= amTes;
                //avarage of rest
                for (int i = 0; i < amRest; i++)
                {
                    avarageRest += rest[subject, i];
                }
                avarageRest /= amRest;
                //avarage all
                double avarage = (avarageRest * 0.5) + (avarageTes * 0.5);
                Console.WriteLine("Your grade avarage is " + avarage);
                Console.ReadLine();
    
                string[] createText = new string[subjects.Length];
               
      
                for (int i = 0; i < subjects.Length; ++i)
                {
                    createText[i] = subjects[i];
                    for (int j = 0; j < amTes; j++) createText[i] += (";" + test[i, j]);
    
                    for (int j = 0; j < amRest; j++) createText[i] += (";" + rest[i, j]);
                }
                    File.WriteAllLines(path + "\\" + "MatFlo.csv", createText, Encoding.UTF8);
    
                
            }
         }
    }
    
    

    Friday, February 1, 2019 10:05 AM

All replies

  • Hello,

    Pay attention to 

    File.AppendAllLines()


    Sincerely, Highly skilled coding monkey.

    • Proposed as answer by DerChris88 Saturday, February 2, 2019 9:49 PM
    Friday, February 1, 2019 10:28 AM
  • Hi Toxjan, your questions seems about C#.  This forum is for Microsoft Small Basic.

    So, I'd like to move this thread to appropriate forum.

    Thanks.


    Nonki Takahashi

    Friday, February 1, 2019 2:32 PM
  • Hi Toxjan,

    >>Now whenever i start the program and put new grades the old ones disapear.

    If you want to add new lines under the old ones, as Andrey Belyakov mentioned, replace WriteAllLines method to AppendAllLines. like this:

    //other code. 
    for (int i = 0; i < subjects.Length; ++i)
                {
                    createText[i] = subjects[i];
                    for (int j = 0; j < amTes; j++) createText[i] += (";" + test[i, j]);
    
                    for (int j = 0; j < amRest; j++) createText[i] += (";" + rest[i, j]);
                }
    //comment it.
    //File.WriteAllLines(path + "\\" + "MatFlo.csv", createText, Encoding.UTF8);
    
    //replace with.
    File.AppendAllLines(path + "\\" + "MatFlo.csv", createText, Encoding.UTF8);
                

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, February 13, 2019 2:24 AM
    Monday, February 4, 2019 3:33 AM
    Moderator