[英]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.