File.ReadAllLines问题 [英] File.ReadAllLines problems

查看:104
本文介绍了File.ReadAllLines问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我写了一个简单的代码,可以保存你的成绩,但我无法阅读它们并保存所有以前的输入。我不知道如何正确使用File.ReadAllLines,因此它读取数据并保存,以后我无法添加新的成绩。现在每当
我启动程序并把新成绩放弃时,旧的成绩会消失。


使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.IO;
使用System.Text;

namespace ConsoleApplication1
{
class program
{
static int ReadGrade()
{
int x;
try
{
x = Convert.ToInt32(Console.ReadLine());
if(x< 1 || x> 6)
{
抛出新的ApplicationException("''无效输入!'");
}
}

catch(例外e)
{
Console.WriteLine(" Error" + e.Message);
x = 0;
}
返回x;
}

static float mtlGrade(float [] x)
{
float mw = x [0];
for(int i = 1; i< x.Length; i ++)mw + = x [i];
返回mw / x.Length;
}

static void Main(string [] args)
{
int amRest = 3; //金额休息
int amTes = 4; // amout test
float [] help = new float [amTes];

// subject
string [] subjects = {" German","English","Math","ITS","SA","Sport"等。 ,"宗教",
"Politik","WL","Electrotechnik"};
int [,] test = new int [subjects.Length,amTes];
int [,] rest = new int [subjects.Length,amRest];

string path = @" c:\ temp" ;;
//读取数据
if(File.Exists(path +" \\" +" MatFlo.csv"))
{
string [] readText = File.ReadAllLines(path +" \\" +" MatFlo.csv",Encoding.UTF8);
string [] row;
//将所有数据保存在"s"中
foreach(readText中的字符串s)
{
//保存数据
for(int i = 0; i< subjects.Length; i ++)
{
row = s.Split(';');
subject [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();

//列出主题
for(int i = 0; i< 10; i ++)
{
if(i< 9)
{
Console.WriteLine((i + 1)+":" + subjects [i]);
}
else
{
Console.WriteLine((i + 1)+":" + subjects [i]);
}
}
//选择主题
int subject; //主题的数量
subject = Convert.ToInt32(Console.ReadLine());学科 - ;
Console.Clear();
Console.WriteLine(" Du hast das subject" subject [subject] +"gewählt!" +" \ n");

//输入测试
for(int i = 0; i< amTes; i ++)
{
Console.Write((i + 1)+" ; Klausur注:");
test [subject,i] = ReadGrade();
help [i] = test [subject,i];
}
Console.WriteLine(" \ n");

//输入休息
for(int i = 0; i< amRest; i ++)
{
Console.Write((i + 1)+" ; restige注:");
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;
//平均休息
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("你的等级平均值是+ + avarage);
Console.ReadLine();

string [] createText = new string [subjects.Length];


for(int i = 0; i< subjects.Length; ++ i)
{
createText [i] =臣[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(路径+" \\" +" MatFlo.csv",createText,Encoding.UTF8);


}
}
}

解决方案

你好,


注意 



文件。追加全部线()


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);

            
        }
     }
}

解决方案

Hello,

Pay attention to 

File.AppendAllLines()


这篇关于File.ReadAllLines问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆