从文本文件中读取和返回数据(C#)

时间:2015-11-09 19:49:38

标签: c# file text streamreader

我正在尝试创建一个程序来读取文本文件并从中返回数据(在本例中为货币)。文本文件包含以这种方式列出的所有货币:

USD US Dollars (USA)               1,077600 1,058100    1,097100
JPY Yen (Japan)                    133,080000   130,480000  135,680000
... etc.

因此,当用户输入货币代码(假设为JPY)时,程序将打印:

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();
    }
}

现在,它返回正确的行,但循环在第一次输入后中断,如果输入空字符串,则返回文本文件的每一行。

任何想法如何继续制作?另外,我应该使用ReadAllLines而不是StreamReader吗?

3 个答案:

答案 0 :(得分:2)

ReadAllLines与StreamReader

如果你的文本文件真的很大,那么比逐行扫描文件是一个很好的做法。在任何其他情况下ReadAllLines都是更好的选择,尤其是在多次扫描时。

空字符串返回结果

您正在使用Contains方法,该方法在字符串内搜索空字符串。 每个字符串都有一个空字符串。

解决方案:测试以查看用户输入空字符串的任何内容并按照您认为合适的方式处理。

只输入一次

您只搜索一次输入,而不是猜测您希望它再次重新扫描。

解决方案:制作另一个循环,它将包裹当前循环,直到你需要它停止。

答案 1 :(得分:2)

这应该以最有效的方式解决它:

   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 = "";

    }

}

答案 2 :(得分:1)

您可以使用以下代码。内联说明

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();
}

enter image description here

相关问题