从文本文件中读取 return 数据 (C#)
Read and return data from text file (C#)
我正在尝试制作一个程序来读取文本文件和 returns 数据(在本例中为货币)。文本文件包含以这种方式列出的所有货币:
USD US Dollars (USA) 1,077600 1,058100 1,097100
JPY Yen (Japan) 133,080000 130,480000 135,680000
... etc.
所以当用户输入货币代码(比如日元)时,程序会打印:
JPY Yen (Japan) 133,080000 130,480000 135,680000
之后,程序将循环并不断询问货币,直到用户输入一个空字符串。
这是我现在拥有的:
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.IO;
public class Currencies
{
public static void Main()
{
string line;
System.IO.StreamReader currencies = new System.IO.StreamReader("currencies.txt");
Console.Write("Enter currency code >");
string currency = Console.ReadLine();
while ((line = currencies.ReadLine()) != null)
{
if (line.Contains(currency))
{
Console.WriteLine(line);
}
}
Console.ReadLine();
}
}
现在,它 returns 是正确的行,但是在第一次输入后循环中断,如果您输入空字符串,它 returns 文本文件的每一行。
有什么想法可以继续制作吗?另外,我应该使用 ReadAllLines 而不是 StreamReader 吗?
ReadAllLines 与 StreamReader
如果您的文本文件确实很大,逐行扫描文件是一个很好的做法。在任何其他情况下 ReadAllLines
将是一个更好的选择,尤其是在多次扫描时。
空字符串returns 结果
您正在使用 Contains
方法,该方法会依次在字符串中搜索空字符串。 每个 字符串内部都有一个空字符串。
解决方案:测试以查看用户输入的任何空字符串并按照您认为合适的方式处理它。
只输入一次
您只搜索一次输入,它无法猜测您希望它再次重新扫描。
解决方案:创建另一个循环,它将环绕当前循环,直到您需要它停止。
这应该是最有效的解决方法:
public static void Main()
{
string line;
bool IsEmptyString = false;
List<string> lines = new List<string>();
using (System.IO.StreamReader currencies = new System.IO.StreamReader("currencies.txt")
{
while ((line = currencies.ReadLine()) != null)
{
lines.Add(line);
}
}
while (!IsEmptyString)
{
string tempLine = "";
Console.Write("Enter currency code >");
string currency = Console.ReadLine();
IsEmptyString = currency == "" ? true : false;
tempLine = lines.FirstOrDefault(x => x.Contains(currency));
if (tempLine!="")
{
Console.WriteLine(tempLine);
}
tempLine = "";
}
}
您可以使用以下代码。内联解释
public static void Main()
{
//read all lines from file only once, and keep for future use
var currencyDetails = File.ReadAllLines(@"C:\YourDirectory\currencies.txt");
string input = string.Empty;
while (true) //keep looping until empty input by user
{
Console.Write("Enter currency code > ");
input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input)) //stop loop
break;
//from all lines of file, get the line where line starts with the user input e.g. usd
var currencyDetail = currencyDetails.FirstOrDefault(l => l.StartsWith(input.ToUpper()));
if (currencyDetail != null) //if a matching currency is found, show it
{
Console.WriteLine(currencyDetail);
}
}
Console.ReadLine();
}
我正在尝试制作一个程序来读取文本文件和 returns 数据(在本例中为货币)。文本文件包含以这种方式列出的所有货币:
USD US Dollars (USA) 1,077600 1,058100 1,097100
JPY Yen (Japan) 133,080000 130,480000 135,680000
... etc.
所以当用户输入货币代码(比如日元)时,程序会打印:
JPY Yen (Japan) 133,080000 130,480000 135,680000
之后,程序将循环并不断询问货币,直到用户输入一个空字符串。
这是我现在拥有的:
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.IO;
public class Currencies
{
public static void Main()
{
string line;
System.IO.StreamReader currencies = new System.IO.StreamReader("currencies.txt");
Console.Write("Enter currency code >");
string currency = Console.ReadLine();
while ((line = currencies.ReadLine()) != null)
{
if (line.Contains(currency))
{
Console.WriteLine(line);
}
}
Console.ReadLine();
}
}
现在,它 returns 是正确的行,但是在第一次输入后循环中断,如果您输入空字符串,它 returns 文本文件的每一行。
有什么想法可以继续制作吗?另外,我应该使用 ReadAllLines 而不是 StreamReader 吗?
ReadAllLines 与 StreamReader
如果您的文本文件确实很大,逐行扫描文件是一个很好的做法。在任何其他情况下 ReadAllLines
将是一个更好的选择,尤其是在多次扫描时。
空字符串returns 结果
您正在使用 Contains
方法,该方法会依次在字符串中搜索空字符串。 每个 字符串内部都有一个空字符串。
解决方案:测试以查看用户输入的任何空字符串并按照您认为合适的方式处理它。
只输入一次
您只搜索一次输入,它无法猜测您希望它再次重新扫描。
解决方案:创建另一个循环,它将环绕当前循环,直到您需要它停止。
这应该是最有效的解决方法:
public static void Main()
{
string line;
bool IsEmptyString = false;
List<string> lines = new List<string>();
using (System.IO.StreamReader currencies = new System.IO.StreamReader("currencies.txt")
{
while ((line = currencies.ReadLine()) != null)
{
lines.Add(line);
}
}
while (!IsEmptyString)
{
string tempLine = "";
Console.Write("Enter currency code >");
string currency = Console.ReadLine();
IsEmptyString = currency == "" ? true : false;
tempLine = lines.FirstOrDefault(x => x.Contains(currency));
if (tempLine!="")
{
Console.WriteLine(tempLine);
}
tempLine = "";
}
}
您可以使用以下代码。内联解释
public static void Main()
{
//read all lines from file only once, and keep for future use
var currencyDetails = File.ReadAllLines(@"C:\YourDirectory\currencies.txt");
string input = string.Empty;
while (true) //keep looping until empty input by user
{
Console.Write("Enter currency code > ");
input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input)) //stop loop
break;
//from all lines of file, get the line where line starts with the user input e.g. usd
var currencyDetail = currencyDetails.FirstOrDefault(l => l.StartsWith(input.ToUpper()));
if (currencyDetail != null) //if a matching currency is found, show it
{
Console.WriteLine(currencyDetail);
}
}
Console.ReadLine();
}