【发布时间】:2021-08-06 21:26:43
【问题描述】:
我是 C# 新手。我有一个文本文件,其中包含:
[False False False]
[ True False False]
[ True False False]
我如何编写一个在控制台上打印的函数,例如如果该行是“[False False False]”它打印为 0,如果该行是 [True False False] 它打印 1,所以它将是 0 1 1 行。我已经阅读了文本文件并通过函数将其逐行打印到控制台:
public static void PrintData()
{
using (var readtext = new StreamReader(@"E:\text.txt"))
{
while (!readtext.EndOfStream)
{
string[] lines = File.ReadAllLines(@"E:\text.txt");
foreach (string line in lines)
Console.WriteLine(line);
}
}
}
【问题讨论】:
-
if (line.IndexOf("True") > 0) Console.WriteLine("1"); -
StreamReader(@"E:\text.txt")将打开文件,然后File.ReadAllLines(@"E:\text.txt")将再次打开文件。你不应该这样做,使用readtect.Readline。
标签: c# if-statement console txt