【问题标题】:StreamReader does not read in carriage returnsStreamReader 不读取回车符
【发布时间】:2013-08-07 19:42:38
【问题描述】:

我必须为我正在学习的计算机课程编写一个控制台应用程序。程序使用 StreamReader 从文件中读取文本,将字符串拆分为单个单词并将它们保存在 String 数组中,然后将单词向后打印。

只要文件中有回车,文件就会停止读取文本。谁能帮我解决这个问题?

这里是主程序:

using System;
using System.IO;
using System.Text.RegularExpressions;

namespace Assignment2
{
    class Program
    {
        public String[] chop(String input)
        {
            input = Regex.Replace(input, @"\s+", " ");
            input = input.Trim();

            char[] stringSeparators = {' ', '\n', '\r'};
            String[] words = input.Split(stringSeparators);

            return words;
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            StreamReader sr = new StreamReader("input.txt");
            String line = sr.ReadLine();

            String[] splitWords = p.chop(line);

            for (int i = 1; i <= splitWords.Length; i++)
            {
                Console.WriteLine(splitWords[splitWords.Length - i]);
            }

            Console.ReadLine();

        }
    }
}

这里是文件“input.txt”:

This is the file you can use to 
provide input to your program and later on open it inside your program to process the input.

【问题讨论】:

  • 嗯...sr.ReadLine() 也许?
  • 需要循环调用ReadLine()依次处理每一行,直到返回null表示EOF。

标签: c# streamreader


【解决方案1】:

您可以使用StreamReader.ReadToEnd 代替StreamReader.ReadLine

// Cange this:
// StreamReader sr = new StreamReader("input.txt");
// String line = sr.ReadLine();

string line;
using (StreamReader sr = new StreamReader("input.txt"))
{
    line = sr.ReadToEnd();
}

添加using 块将确保输入文件也正确关闭。

另一种选择就是使用:

string line = File.ReadAllText("input.txt"); // Read the text in one line

ReadLine 从文件中读取单行,并去掉尾随的回车符和换行符。

ReadToEnd 会将整个文件作为单个字符串读取,并保留这些字符,从而使您的 chop 方法能够按写入方式工作。

【讨论】:

  • 谢谢一百万。您的解决方案完全没有任何麻烦。 :D
  • 我认为使用File.ReadAllText() 会更容易,例如var line = File.ReadAllText("input.txt");
【解决方案2】:

你只是在读一行。您需要阅读所有行直到文件结束。 以下应该有效:

    String line = String.Empty;
    using (StreamReader sr = new StreamReader("input.txt"))
    {
        while (!sr.EndOfStream)
        {
            line += sr.ReadLine();
        }
    }

【讨论】:

  • 什么是Programchop 在哪里定义?如果程序是包含 main 创建实例的类很奇怪...我认为您应该在线使用System.String.Split(char delim)...
  • 非常感谢您的帮助。您所做的方式工作得很好,但我被告知我应该使用使用更少代码的方式。不过非常感谢您的回答
  • @evanmcdonald 完全同意..我只是在解决读取所有行的问题..其余的我没有注意,因为我只是复制了 OP 所做的事情。这些更改需要向 OP 提出建议。我将编辑我的答案,只显示我想要解决的相关部分,以免误导 OP。
  • 很高兴您的问题得到解决。谢谢鼓励。您应该查看@evanmcdonald 在上述 cmets 中提出的建议。
  • @evanmcdonnal 我将 Chop 更改为静态,现在它不需要调用对象。我必须从应用程序中删除多余的空格,以便每个单词都显示在前一个单词下面,而不会有任何间隔
【解决方案3】:

问题是您正在调用 ReadLine() ,它就是这样做的,它会一直读取直到遇到回车(您必须在循环中调用它)。

通常,如果您想使用 StreamReader 逐行读取文件,则实现看起来更像这样(来自 msdn);

        using (StreamReader sr = new StreamReader("TestFile.txt")) 
        {
            string line;
            // Read and display lines from the file until the end of  
            // the file is reached. 
            while ((line = sr.ReadLine()) != null) 
            {
                Console.WriteLine(line);
            }
        }

while 循环中的条件确保您将读取到文件末尾,因为如果没有可读取的内容,ReadLine 将返回 null

另一种选择是使用File.ReadAllLines(MyPath),它将返回一个字符串数组,每个元素是文件中的一行。举一个更完整的例子;

  string[] lines = File.ReadAllLines(MyFilePath);
  foreach(string line in lines)
  {
        string[] words = line.Split(' ').Reverse();
        Console.WriteLine(String.Join(" ", words));
  }

这三行代码执行以下操作;将整个文件读入一个字符串数组,其中每个元素都是一行。循环遍历该数组,在每一行上,我们将其拆分为单词并颠倒它们的顺序。然后我将所有单词与它们之间的空格重新连接在一起并将其打印到控制台。如果您希望整个文件以相反的顺序开始,那么您需要从最后一行而不是第一行开始,我将把这个细节留给您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多