程序从文件中读取前五个单词而不读取C#中的完整文件 [英] Program to read first five words from a file without reading the full file in C#
本文介绍了程序从文件中读取前五个单词而不读取C#中的完整文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不想阅读整个文件。我只是希望我的程序从文件中读出前五个单词。
我尝试过:
i使用readalllines和readalltext ..但它读取整个文件
i do not want to read the entire file. i just want my program to read first five words from the file.
What I have tried:
i used readalllines and readalltext.. but it read the whole file
推荐答案
TextReader很好: [ ^ ]
来自链接的例子:
TextReader is nice:[^]
example from link:
using (TextReader reader = File.OpenText(@"C:\perl.txt"))
{
string line = reader.ReadLine();
Console.WriteLine(line);
}
所以读取char-by-char并在5个字后停止...
StreamReader.Read方法(Char [],Int32,Int32)(系统。 IO) [ ^ ]
So read it char-by-char and stop after 5 words...
StreamReader.Read Method (Char[], Int32, Int32) (System.IO)[^]
重复使用 Readline
,直到获得所需的单词。例如,请参阅:如何:一次读取一行文本文件(Visual C#)| Microsoft Docs [ ^ ]。
Repeadly use Readline
, until you get the required words. See, for instance: How to: Read a Text File One Line at a Time (Visual C#) | Microsoft Docs[^].
这篇关于程序从文件中读取前五个单词而不读取C#中的完整文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文