【发布时间】: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