C#读取并从.txt拆分为结构数组
我正在尝试为我的控制台应用程序进行基本登录。我将用户数据存储在.txt文件中,如下所示:C#读取并从.txt拆分为结构数组,c#,arrays,file,text,struct,C#,Arrays,File,Text,Struct,我正在尝试为我的控制台应用程序进行基本登录。我将用户数据存储在.txt文件中,如下所示: ID;名称IsAdmin。文本有几行 在应用程序中,我想将用户数据存储在struct user数组中。我似乎找不到读取文件、拆分并将不同数据放到正确位置的方法。这就是我到目前为止所做的: 正在将用户数据加载到结构数组 publicstaticvoidloadids() { int entries=FileHandling.CountRows(usersPath); User[]users=new User[
ID;名称IsAdmin
。文本有几行
在应用程序中,我想将用户数据存储在struct user
数组中。我似乎找不到读取文件、拆分并将不同数据放到正确位置的方法。这就是我到目前为止所做的:
正在将用户数据加载到结构数组
publicstaticvoidloadids()
{
int entries=FileHandling.CountRows(usersPath);
User[]users=new User[entries];//长度取决于.txt中的行数
for(int i=0;i
阅读和拆分文本
公共静态字符串ReadFileToArray(字符串路径)
{
字符串输入=File.ReadAllText(路径);
foreach(input.Split('\n')中的var记录)
{
foreach(record.Split(“;”)中的变量数据)
{
返回数据;
}
}
返回null;
}
我知道这样做根本不起作用,但我的知识有限,我想不出其他解决方案。您有更好的工具来存储用户。您可以使用一个列表来代替数组(强制您知道加载的数据的长度),在其中您可以在读取元素时添加元素 另一个需要更改的点是File.ReadLines中的File.ReadAllText。这将允许直接在循环中逐行读取文件
public List<User> BuildUserList(string path)
{
List<User> result = new List<User>();
foreach (var record in File.ReadLines(path)
{
string[] data = record.Split(';'))
User current = new User();
current.ID = Convert.ToInt32(data[0]);
current.Name = data[1];
current.IsAdmin = Convert.ToBoolean(data[2]);
result.Add(current);
}
return result;
}
public List BuildUserList(字符串路径)
{
列表结果=新列表();
foreach(File.ReadLines(路径)中的var记录)
{
string[]data=record.Split(“;”)
用户当前=新用户();
current.ID=Convert.ToInt32(数据[0]);
current.Name=数据[1];
current.IsAdmin=Convert.ToBoolean(数据[2]);
结果。添加(当前);
}
返回结果;
}
现在,如果需要,可以像使用数组一样使用列表
List<User> users = BuildUserList("yourfile.txt");
if(users.Count > 0)
{
Console.WriteLine("Name=" + users[0].Name);
}
List users=BuildUserList(“yourfile.txt”);
如果(users.Count>0)
{
Console.WriteLine(“Name=“+users[0].Name”);
}
如果我假设你的文件,尤其是每一行都有Id;名称Admin
values,我将编写如下内容来提取它。请注意,这里有简单的语法,但下面的逻辑将有助于初学者理解如何实现这一点
List<User> userList = new List<User>();
// Read the file located at c:\test.txt (this might be different in your case)
System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
string line;
while ((line = file.ReadLine()) != null)
{
//following logic will read each line and split by the separator before
// creating a new User instance. Remember to add more defensive logic to
// cover all cases
var extract = line.Split(';');
userList.Add(new User()
{
Id = extract[0],
Name = extract[1],
IsAdmin = extract[2]
});
}
file.Close();
//at this stage you will have List of User and converting it to array using following call
var userArray = userList.ToArray();
List userList=newlist();
//读取位于c:\test.txt的文件(在您的情况下可能会有所不同)
System.IO.StreamReader file=new System.IO.StreamReader(@“c:\test.txt”);
弦线;
而((line=file.ReadLine())!=null)
{
//下面的逻辑将读取每一行,并在之前通过分隔符进行拆分
//正在创建新的用户实例。请记住向
//包罗万象
var extract=line.Split(“;”);
添加(新用户()
{
Id=提取[0],
名称=摘录[1],
IsAdmin=摘录[2]
});
}
file.Close();
//在此阶段,您将获得用户列表,并使用以下调用将其转换为数组
var userArray=userList.ToArray();
作为另一种变体,linq解决方案可能如下所示:
var users = (
from string line in System.IO.File.ReadAllLines(@"..filepath..")
let parts = line.Split(';')
where parts.Length == 3
select new User() {
ID = Convert.ToInt32(parts[0]),
Name = parts[1],
IsAdmin = Convert.ToBoolean(parts[2])}
).ToArray();
这可能是优雅和简短的,错误处理可能会有点困难。这将延迟读取您的文件,因此它可以轻松地处理非常大的文件(假设您的其余代码可以):
public IEnumerable可读用户(字符串路径)
{
返回文件.ReadLines(路径)
.选择(l=>l.Split(“;”))
.选择(l=>新用户
{
Id=int.Parse(l[0]),
Name=l[1],
IsAdmin=bool.Parse(l[2])
});
}
或
public IEnumerable可读用户(字符串路径)
{
返回文件.ReadLines(路径)
.选择(l=>l.Split(“;”))
.Select(l=>newuser(int.Parse(l[0]),l[1],bool.Parse(l[2]));
}
您必须逐行读取文件,并为每个lineUse文件创建一个用户
实例。ReadAllLines而不是ReadAllText思考您的ReadFileToArray
正在做什么。。按行拆分,然后按拆分每行代码>,然后您只需返回第一件事。。。你为什么要这么做?如果你想保持它的基本性,你就太复杂了。一起跳过文件读取,只将数据存储在内存中。使用file.ReadLines
而不是file.ReadAllLines
public IEnumerable<User> ReadUsers(string path)
{
return File.ReadLines(path)
.Select(l=>l.Split(';'))
.Select(l=> new User
{
Id = int.Parse(l[0]),
Name = l[1],
IsAdmin = bool.Parse(l[2])
});
}
public IEnumerable<User> ReadUsers(string path)
{
return File.ReadLines(path)
.Select(l=>l.Split(';'))
.Select(l=> new User(int.Parse(l[0]), l[1], bool.Parse(l[2])));
}