C# 如何使用c将列表写入和读取到包含用户输入的文本文件#
我正在开发控制台应用程序,我正在尝试将列表保存到txt文件,并在以后读取该数据 在程序中,用户输入类别的名称,我不知道如何将该名称和列表保存在txt文件中 包含名称的结构类别C# 如何使用c将列表写入和读取到包含用户输入的文本文件#,c#,C#,我正在开发控制台应用程序,我正在尝试将列表保存到txt文件,并在以后读取该数据 在程序中,用户输入类别的名称,我不知道如何将该名称和列表保存在txt文件中 包含名称的结构类别 struct Category { public string name; } 这是到目前为止我的代码 Category k; Console.WriteLine("Enter name of category: "); k.name = Console.ReadLine(); List<String&
struct Category
{
public string name;
}
这是到目前为止我的代码
Category k;
Console.WriteLine("Enter name of category: ");
k.name = Console.ReadLine();
List<String> category = new List<string>();
TextWriter tw = new StreamWriter("../../dat.txt");
foreach (string k in category)
{
string[] en = s.Split(',');
category.Add(k.name); // Here I am not sure how to save name
}
tw.Close();
StreamReader sr = new StreamReader("../../dat.txt");
string data = sr.ReadLine();
while (data != null)
{
Console.WriteLine(data);
data = sr.ReadLine();
}
sr.Close();
k类;
Console.WriteLine(“输入类别名称:”);
k、 name=Console.ReadLine();
列表类别=新列表();
TextWriter tw=新的StreamWriter(“../../dat.txt”);
foreach(类别中的字符串k)
{
字符串[]en=s.Split(',');
category.Add(k.name);//这里我不知道如何保存name
}
tw.Close();
StreamReader sr=新的StreamReader(“../../dat.txt”);
字符串数据=sr.ReadLine();
while(数据!=null)
{
控制台写入线(数据);
data=sr.ReadLine();
}
高级关闭();
它并没有给我任何错误,但它并没有将名称写入txt文件
SOLUTIN
stringfilepath=@./../datoteka.txt”;
List kategorije=File.ReadAllLines(filePath.ToList();
foreach(kategorije中的字符串s)
{
控制台。写入线(s);
}
kategorije.增补(k.naziv);
writeAllines(filePath,kategorije);
您可以使用System.IO.File
类的静态方法。它们的优点是可以自动打开和关闭文件,从而将文件的写入和读取任务简化为一条语句
File.WriteAllLines(yourFilePath, category);
您可以使用
category = new List(ReadLines(yourFilePath));
ReadLines
返回一个IEnumerable
,它在列表的构造函数中被接受为数据源
或者使用
string[] array = ReadAllLines(yourFilePath);
您的解决方案不会向输出流写入任何内容。您正在初始化
TextWriter
,但未使用它。你会像这样使用它
tw.WriteLine(someString);
您的代码有一些问题:您正在声明一个类别变量k
,但从未为其分配类别。您的列表不属于类别类型
更好的解决方案应该是这样的
var categories = new List<Category>(); // Create a categories list.
while (true) { // Loop as long as the user enters some category name.
Console.WriteLine("Enter name of category: ");
string s = Console.ReadLine(); // Read user entry.
if (String.IsNullOrWhiteSpace(s)) {
// No more entries - exit loop
break;
}
// Create a new category and assign it the entered name.
var category = new Category { name = s };
//TODO: Prompt the user for more properties of the category and assign them to the
// category.
// Add the category to the list.
categories.Add(category);
}
File.WriteAllLines(yourFilePath, categories.Select(c => c.name));
var categories=new List();//创建一个类别列表。
while(true){//循环,只要用户输入某个类别名称。
Console.WriteLine(“输入类别名称:”);
字符串s=Console.ReadLine();//读取用户条目。
if(String.IsNullOrWhiteSpace){
//没有更多条目-退出循环
打破
}
//创建一个新类别并为其指定输入的名称。
变量类别=新类别{name=s};
//TODO:提示用户输入类别的更多属性,并将其分配给
//类别。
//将类别添加到列表中。
类别。添加(类别);
}
writeAllines(您的文件路径,categories.Select(c=>c.name));
类别
类型应为class。请参阅:您没有使用WtiteLine编写内容。之后在解决方案中添加以下代码
category.Add(k.name);
tw.WriteLine(someString);
查找下面的读写示例代码
class WriteTextFile
{
static void Main()
{
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
string text = "A class is the most powerful data type in C#. Like a structure, " +
"a class defines the data and behavior of the data type. ";
System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
{
foreach (string line in lines)
{
if (!line.Contains("Second"))
{
file.WriteLine(line);
}
}
}
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
{
file.WriteLine("Fourth line");
}
}
}
实际上,你不会使用
TextWriter
写任何东西,也就是说,我希望看到tw.WriteLine(k.Name)
或类似的东西。阅读方法可能重复:在category.Add(k.Name)
添加一行tw.WriteLine(k.Name)
。有很多问题:你从未使用tw
来实际写任何东西;您尝试通过一个新的空列表foreach
;将k
定义为Category
struct,然后再次定义为foreach
变量;无论s
中有什么s.Split
都不会在显示的代码中定义;列表
包含字符串
值,而不是您的类别
结构…感谢您的回答。我设法根据它修复了我的代码。如果你检查我的问题,我会把它作为解决方案。:)@奥利维尔·雅科特·德斯科姆贝斯
class WriteTextFile
{
static void Main()
{
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
string text = "A class is the most powerful data type in C#. Like a structure, " +
"a class defines the data and behavior of the data type. ";
System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
{
foreach (string line in lines)
{
if (!line.Contains("Second"))
{
file.WriteLine(line);
}
}
}
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
{
file.WriteLine("Fourth line");
}
}
}