C#如何将列表中的输入数据保存到.txt文件中 [英] C# how can I save my input datas in list to .txt file
问题描述
我需要将数据保存在我在控制台中输入的列表中。我有3个函数`Show(),Add(),Delete()。当我输入1时它必须显示我的数据列表,当我输入2它必须问我用逗号输入新用户的数据,例如`John,25,然后它必须保存在我的列表中,也就是说当我输入1它必须显示我的列表+新添加数据,当我输入3时它必须问我请输入要删除的人的索引,例如我将输入2并且必须删除第二个索引人,然后当我输入1时,它必须显示我没有第二个索引人的列表。
但是在我的代码中它不起作用,在添加或删除之后它没有显示我添加或删除的那个人。
请帮帮我,我的错误是什么???????
我尝试了什么:
i need to save data in list which i input in console.I have 3 function`Show(),Add(),Delete(). When i input 1 it must show me my data's list,when i input 2 it must ask me "Enter new user's data with comma",for example` John,25,then it must be saved in my list, that's to say when i input 1 it must show me my list + new adding data, and when i input 3 it must ask me "Please type the index of Person, whom you want to delete",for example i will input 2 and it must delete second index person, and then when i input 1 it must show me my list without second index person.
But in my code it's not working, after adding or deleting it's not show me that person whom i added or deleted.
Please help me,What's my mistake???????
What I have tried:
class Controller
{
public List<Person> all;
public void Show()
{
string path = "C:/Users/User/Desktop/mardik.txt";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
List<Person> all = bf.Deserialize(fs) as List<Person>;
foreach (Person item in all)
{
Console.WriteLine(item.name + " " + item.age);
}
fs.Close();
}
public void Add()
{
string path = "C:/Users/User/Desktop/mardik.txt";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryFormatter bf = new BinaryFormatter();
List<Person> all = bf.Deserialize(fs) as List<Person>;
Console.WriteLine("Enter new user's data with comma");
string text = Console.ReadLine();
string[] segments = text.Split(',');
string a = segments[0];
int b;
bool hajoxvec = int.TryParse(segments[1], out b);
if (!hajoxvec)
{
Console.WriteLine("Please try again");
this.Add();
}
else
{
all.Add(new Person(a, b));
}
foreach (Person item in all)
{
Console.WriteLine(item.name + " " + item.age);
}
fs.Close();
}
public void Delete()
{
string path = "C:/Users/User/Desktop/mardik.txt";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryFormatter bf = new BinaryFormatter();
List<Person> all = bf.Deserialize(fs) as List<Person>;
Console.WriteLine();
Console.WriteLine("Please type the index of Person, whom you want to delete");
int num = int.Parse(Console.ReadLine());
if (num < 0 || num > 4)
{
Console.WriteLine("Please try again");
this.Delete();
}
else
{
all.RemoveAt(num);
}
foreach (Person item in all)
{
Console.WriteLine(item.name + " " + item.age);
}
fs.Close();
}
}
[Serializable]
class Person
{
public string name;
public int age;
public Person(string a, int b)
{
this.name = a;
this.age = b;
}
}
static void Main(string[] args)
{
string path = "C:/Users/User/Desktop/mardik.txt";
//List<Person> mardik = new List<Person>();
//mardik.Add(new Person("Valod", 20));
//mardik.Add(new Person("Petros", 23));
//mardik.Add(new Person("Poghos", 25));
//mardik.Add(new Person("Hranush", 22));
//mardik.Add(new Person("Suren", 18));
//FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
//BinaryFormatter bf = new BinaryFormatter();
//bf.Serialize(fs, mardik);
//fs.Close();
Controller c = new Controller();
while (true)
{
Console.WriteLine();
Console.WriteLine("Incheq uzum anel:\n1.Show all People\n2.Add new person\n3.Delete any person");
int x = int.Parse(Console.ReadLine());
switch (x)
{
case 1:
c.Show();
break;
case 2:
c.Add();
break;
case 3:
c.Delete();
break;
}
}
}
推荐答案
问题是否有任何意义,如果你是只是忽略我们告诉你的一切?
如何使用switch-case 将列表中的数据添加到.txt文件[ ^ ]
当然,回答它们似乎没什么意义......
Is there any point in asking questions, if you are just going to ignore everything we tell you?
How to add data in list to .txt file with switch-case[^]
Certainly, there appears to be little point in answering them...
这篇关于C#如何将列表中的输入数据保存到.txt文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!