如何每4行读取和创建新用户

时间:2015-11-25 17:57:54

标签: c#

所以,我读了一个文本文件。它看起来像这样:

TEACHER - TEACHER/STUDENT
adamsmith - ID
Adam Smith - Name
B1u2d3a4 - Password
STUDENT
marywilson
Mary Wilson
s1Zeged
TEACHER
sz12gee3
George Johnson
George1234
STUDENT
sophieb
Sophie Black
SophieB12

依此类推,有所有用户。

用户类:

class User
{
    private string myID;
    private string myName;
    private string myPW;
    private bool isTeacher;

    public string ID
    {
        get
        {
            return myID;
        }
        set
        {
            myID = value;
        }
    }

    public string Name
    {
        get
        {
            return myName;
        }
        set
        {
            myName = value;
        }
    }

    public string PW
    {
        get
        {
            return myPW;
        }
        set
        {
            PW = value;
        }
    }

    public bool teacher
    {
        get
        {
            return teacher;
        }
        set
        {
            isTeacher = value;
        }
    }

    public override string ToString()
    {
        return myName;
    }
}

Form1_Load 方法:

private void Form1_Load(object sender, EventArgs e)
{
    List<User> users = new List<User>();
    string line;

    using (StreamReader sr = new StreamReader("danet.txt"))
    {
        while ((line=sr.ReadLine())!=null)
        {
            User user = new User();
            user.ID=line;
            user.Name=sr.ReadLine();
            user.PW=sr.ReadLine();

            if(sr.ReadLine=="TEACHER")
            {
                teacher=true;
            }
            else
            {
                teacher=false;
            }

            users.Add(user);
        }
    }
}

我想阅读文本并存储信息。通过这种方法,我得到的用户数比我应该多4倍。我正在考虑使用for和一些事情,但我没有找到解决方案。

2 个答案:

答案 0 :(得分:2)

新答案

您的读者假设每第四行是用户ID,而不是,绝对第一行是STUDENT / TEACHER行。要么这是一个错字,要么你必须改变你的格式。

您的PW属性将导致StackOverflowException

        public string PW
        {
            get
            {
                return myPW;
            }
            set
            {
                PW = value; 
            }
        }

将设置者更改为myPW = value;,或者只将其转换为auto-properties。 您的teacher属性具有相同的错误,但在getter上。

您还错过了()之一的ReadLine,但我们假设这是一个错字。

不是使用文本文件,而是使用字符串,所以我使用的是StringReader,但它的概念相同。

        string stuff =
@"adamsmith
Adam Smith
B1u2d3a4
STUDENT
marywilson
Mary Wilson
s1Zeged
TEACHER
sz12gee3
George Johnson
George1234
STUDENT
sophieb
Sophie Black
SophieB12
STUDENT";

    public void Main(string[] args)
    {
        string line;

        var users = new List<User>();

        using (var sr = new StringReader(stuff))
        {
            while ((line = sr.ReadLine()) != null)
            {
                User user = new User();
                user.ID = line;
                user.Name = sr.ReadLine();
                user.PW = sr.ReadLine();
                user.teacher = sr.ReadLine() == "TEACHER";

                users.Add(user);
            }
        }
    }

旧答案

您的代码没有任何内在错误。但是,由于您没有提供“danet.txt”的实际示例,因此必须假设错误位于数据本身内。

你的“解析器”(如果你想把它称之为)是不宽容的,即如果你的源文件中有一个空行或者你只是弄乱了一行(比如忘记输入密码或ID)那么一切都会被抵消 - 但就你的“解析器”而言,没有什么是错的。

默认情况下,依赖于“行位置”或“行偏移”的格式容易中断,尤其如果文件本身是手动创建而不是自动生成。

为什么不使用表示格式?例如XMLJSON甚至只是INI。 C#可以处理其中任何一个,无论是内置库还是外部库(参见链接)。

如果用户输入错误,那么你的“逐行”解析器永远不会有不中断,除非你对ID,名称有非常严格的格式,密码和“学生/老师”。然后使用正则表达式(或类似)验证它们。但这会破坏简单的“逐行”格式的目的。到那时,您可能会选择更“复杂”的格式。

答案 1 :(得分:0)

while ((line=sr.ReadLine())!=null)
{
    User user = new User();
    for (int i = 0; i < 4; i++)
    {
        switch (i)
        {
            case 1:
                user.ID = line;
                break;
            case 2:
                user.Name=sr.ReadLine();
                break;
            ....
        }
    }
}