文本文件的阅览座 [英] Reading Block of text file
问题描述
我具有其中一个特定字符重复在该行的每隔几行之后开始的文本文件。无。介于两者之间是不固定的线路。我能够找出发生这种情况的行。我想读之间的线路。
I have a text file in which a particular character repeats at the start of the line after every few lines. the no. of lines in between is not fixed. I am able to find out those lines where this condition occurs. I want to read those lines in between.
using (StreamReader sr = new StreamReader(@"text file"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith("some character"))
由于接下来的时间,这个人物时,代码保持不变。我不能够之间
Because the next time, this character occurs, the code remains same. I am not able to read those lines in between
阅读这些线如:
Condition at the begining of a line
Next line
Next line
Condition at the begining of a line
Next Line
Next Line
Next Line
Next Line
Condition at the begining of a line
的开头$ b条件
我要读线之间。条件每次都保持不变。谢谢你。
I have to read lines in between. The condition remains same every time. Thanks.
推荐答案
我假设你要分析文本文件和工艺条件,每次之间的所有文本块。
I assume you want to parse the text file and process all the blocks of text between conditions each time.
基本上,你读的每一行,检查的首要条件,这标志着块的开始,并保持阅读的线条,直到你找到另一个条件,这标志着该块(和另一个开始)结束。
Basically you read each line, check for the first condition, which signals the start of a "block" and keep reading the lines until you find another condition, which signals the end of the block (and the start of another).
洗,漂洗,重复。
一个简单例如:
using (var reader = new StreamReader(@"test.txt"))
{
var textInBetween = new List<string>();
bool startTagFound = false;
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (String.IsNullOrEmpty(line))
continue;
if (!startTagFound)
{
startTagFound = line.StartsWith("Condition");
continue;
}
bool endTagFound = line.StartsWith("Condition");
if (endTagFound)
{
// Do stuff with the text you've read in between here
// ...
textInBetween.Clear();
continue;
}
textInBetween.Add(line);
}
}
这篇关于文本文件的阅览座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!