简体   繁体   English

如何从 streamreader 读取第一行的第一个字符? (例如 txt 文件中的字母 H

[英]How can I read from streamreader the first character from the first line? ( Example the Letter H that is in the txt file

I need to read the first char from the first line and then others in a streamreader and if statements.我需要从第一行读取第一个字符,然后在流读取器和 if 语句中读取其他字符。

    private void buttonEdit_Click(object sender, EventArgs e)
    {
        NewSalariedEmployee newSalariedEmployee = new NewSalariedEmployee();
        HourlyEmployeeDetails hourlyEmployeeDetails = new HourlyEmployeeDetails();
        employees = new Employees();
        string firstLine;
        using (StreamReader reader = new StreamReader("employees.txt"))
        {
            firstLine = reader.ReadLine();
        }
        if (firstLine.Contains(firstLine.StartsWith("H"))
        {
            hourlyEmployeeDetails.ShowDialog();
        }
     }

The StreamReader.Read method that "Reads the next character from the input stream and advances the character position by one character." StreamReader.Read方法“从输入 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 读取下一个字符并将字符 position 前进一个字符。” is not what I'm looking for...不是我要找的...

If I understand what you are trying to do (though I'm guessing a bit).如果我了解您要做什么(尽管我有点猜测)。

If you are trying to read each line in a file and process the first character of each line, you just need a loop to keep reading lines until you have read them all.如果您尝试读取文件中的每一行并处理每行的第一个字符,则只需要一个循环来继续读取行,直到您将它们全部读完。 You can use the Peek method to see if you have more to read.您可以使用Peek方法查看是否有更多要阅读的内容。

private void buttonEdit_Click(object sender, EventArgs e)
{
    NewSalariedEmployee newSalariedEmployee = new NewSalariedEmployee();
    HourlyEmployeeDetails hourlyEmployeeDetails = new HourlyEmployeeDetails();
    employees = new Employees();
    using (StreamReader reader = new StreamReader("employees.txt"))
    {
        // see if there is more to read from the reader
        while (reader.Peek() > -1)
        {
            // read the next line and perform your logic on the first char
            string currentLine = reader.ReadLine();
            if (currentLine != null && currentLine.StartsWith("H"))
            {
                hourlyEmployeeDetails.ShowDialog();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么能有一个 .txt 文件,我从中读取行并将它们放入一个数组中,但每一行都在某个字符后被拆分 - How can I have a .txt file that I read lines from and put them into an array, but every line is split after a certain character 有没有办法从StreamReader中删除第一行 - is there a way to remove the first line from a StreamReader StreamReader读取文件,StreamWriter写入结果,省略第一行吗? - StreamReader read file and StreamWriter write that result omitting first line? 使用StreamReader从文件读取数据到第1行的数组中 - read data from file with StreamReader into array of line 1 C#:使用StreamReader从txt文件中读取行,但Peek()返回-1,即使还有很多行 - C#: Using StreamReader to read line from txt file, but Peek() return -1 even there are a lot of lines left 从TXT文件中读取所有行,并且仅在每行C#的每个FIRST字符串中搜索 - Read all lines from TXT file and search only in every FIRST string of each line C# 从内存中读取文件的第一行 - Read the first line of a file from memory 只读取文本文件的第一行 - Read only first line from a text file C#StreamReader从文本文件读取缺少第一行 - C# StreamReader reading from textfile missing the first line 如何从文本文件中读取最后一行的第一个单词? - How to read first word of last line from text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM