我正在逐行读取文本文件,但在消息框中.我总是看到同样的第一行

本文关键字:一行 取文本 读取 逐行 文件 消息 | 更新日期: 2023-09-27 18:08:46

在构造函数中:

binFileToRead = @"d:'DenyTextHaunted.bin";
r = new StreamReader(binFileToRead);
f = r.ReadToEnd();
translatedFile = @"d:'binText_1.txt";
h = new StreamReader(translatedFile);
test();
现在test() 中的代码
private void test()
{
            StreamWriter w = new StreamWriter(@"d:'testFile.txt");
            w.AutoFlush = true;
            int currentLength;
            int currentIndex;
            int lastIndex = 0;
            string startTag = "<Text>";
            string endTag = "</Text>";
            int startTagWidth = startTag.Length;
            //int endTagWidth = endTag.Length;
            int index = 0;
            while ((line = h.ReadLine()) != null)
            {
                while (true)
                {
                    index = f.IndexOf(startTag, index);
                    if (index == -1)
                    {
                        //startTag not found, but endTag may still be found
                        index = f.IndexOf(endTag, lastIndex - 1);
                        if (index != -1)
                        {
                            w.Write(f.Substring(lastIndex, endTag.Length));
                        }
                        break;
                    }
                    // else more to do - index now is positioned at first character of startTag    
                    int start = index + startTagWidth;
                    if (start >= f.Length)
                        break;
                    currentIndex = start;
                    index = f.IndexOf(endTag, start + 1);
                    if (index == -1)
                    {
                        break;
                    }
                    // found the endTag    
                    string g = f.Substring(start, index - start - 1);
                    currentLength = index - start;
                    string hh = f.Substring(lastIndex, currentIndex - lastIndex);
                    w.Write(hh);
                    w.Write(line);
                    MessageBox.Show(line.ToString());
                    listBox2.Items.Add(hh);
                    lastIndex = currentIndex + currentLength;
                    listBox1.Items.Add(g);
                }
                w.Close();
            }
        }

到目前为止,我在test()中所做的是读取一个文件到末尾,然后取出所有文件内容,而不包括标签<Text></Text>之间的文本

并将其写入一个新的文本文件。

例如,新的文本文件看起来像:
hi everyone <Text></Text> and you are...
<Text></Text> by and hello

现在我要做的是从另一个文件中逐行读取变量h并添加while ((line = h.ReadLine()) != null)

然后我将每一行写入<Text></Text>

之间的位置
w.Write(line);

但是在MessageBox.Show(line.ToString());中,我一直看到文本文件的第一行。我一直在阅读和重复,只显示第一行。

为什么它不告诉我下一次迭代的下一个文件?

谢谢。


Guffa它不工作好。我使用了你的修复样本,使用一个while循环逐行读取,但随后写入的新文件从末尾缺少一些行。我不确定问题是否在写或在读取到构造函数的末尾。或者循环不好。

我想做的是读取F的一部分直到读取H的一部分一行然后将这一行放在第一行之间然后F将继续读取到下一行然后从H读取下一行然后将第二行放在

的第二个位置

总的来说它是工作的,概念是工作的,但技术上它不读取或写入新文件几乎到最后,但缺少几行。

private void test()
        {
            StreamWriter w = new StreamWriter(@"d:'testFile.txt");
            w.AutoFlush = true;
            int currentLength;
            int currentIndex;
            int lastIndex = 0;
            string startTag = "<Text>";
            string endTag = "</Text>";
            int startTagWidth = startTag.Length;
            //int endTagWidth = endTag.Length;
            int index = 0;
            while ((line = h.ReadLine()) != null)
            {
                //while (true)
                //{
                    index = f.IndexOf(startTag, index);
                    if (index == -1)
                    {
                        //startTag not found, but endTag may still be found
                        index = f.IndexOf(endTag, lastIndex - 1);
                        if (index != -1)
                        {
                            w.Write(f.Substring(lastIndex, endTag.Length));
                        }
                        break;
                    }
                    // else more to do - index now is positioned at first character of startTag    
                    int start = index + startTagWidth;
                    if (start >= f.Length)
                        break;
                    currentIndex = start;
                    index = f.IndexOf(endTag, start + 1);
                    if (index == -1)
                    {
                        break;
                    }
                    // found the endTag    
                    string g = f.Substring(start, index - start - 1);
                    currentLength = index - start;
                    string hh = f.Substring(lastIndex, currentIndex - lastIndex);
                    w.Write(hh);
                    w.Write(line);
                    listBox2.Items.Add(hh);
                    lastIndex = currentIndex + currentLength;
                    listBox1.Items.Add(g);
                //}
                //w.Close();
            }
            w.Close();
        }

f是构造函数中的一个文件,我一直读到末尾:

binFileToRead = @"d:'DenyTextHaunted.bin";
r = new StreamReader(binFileToRead);
f = r.ReadToEnd();

h是我在构造函数中读取的另一个文本文件,但没有到末尾:

translatedFile = @"d:'binText_1.txt";
h = new StreamReader(translatedFile);

则在函数test()中

我正在创建一个新的文本文件,它应该包含文件F的所有内容,不包括标签和

之间的文本

我将h文件一行一行地写入这个文件:

首先读取F的一部分直到第一行然后从H中读取一行并将这行放在

之间

等等

但是由于某些原因,当我编辑新的文本文件testFile.txt时,我看到它没有写到末尾。

最后一行如下:

嗯闻起来不太糟,但是你没有cirque-red,下面的ü可能会请…嗯……。

但是最后没有,我检查了原始文件,在这行之后有更多的行。

那么为什么只读取原始文件或写入新文件直到这一部分呢?

也许因为我使用的循环是结束时,它完成读取所有行从H文件?

在我使用另一个循环之前它只是While(true){}

但是它没有逐行读取H

我不知道为什么它不写或读到最后。

这个概念是可行的,但从技术上讲它还没有结束。

下面是H读取的示例:

这个桶在这里也帮不了我。这个桶在这里没有任何意义。在这里,怜悯也使桶失去了意义。这个桶跟海报不搭。只有我必须创造好主意,它无论如何画后à en…船k önnte无论如何都使用,绕着桶进入ünden。但不是在水下。第一次不是。

这是我要逐行读取的文件。

另一个文件之间有另一个文本,现在我想像上面解释的那样逐行读取其中一些,然后读取其中一些,然后放入一行,等等。

谢谢。

我正在逐行读取文本文件,但在消息框中.我总是看到同样的第一行

这是因为您有两个循环,并且在外部循环中读取数据。因为你永远不会退出内循环(除非有什么错误),你永远不会得到第二行。

你有这样的结构:

while ((line = h.ReadLine()) != null)
{
  while (true)
  {
    ...
  }
  w.Close();
}

,但你应该有:

while ((line = h.ReadLine()) != null)
{
  ...
}
w.Close();

我不确定这个循环的目的是什么。您不会将line的值用于循环中的任何逻辑,因此您只会一遍又一遍地执行相同的工作,然后一遍又一遍地显示相同的结果,以及从文件中读取的行。

不是回答为什么您的当前解决方案不起作用(为此,请参阅Guffa的回答),我想问为什么要尝试在c#中逐行解析XML作为文本?有更好的解决方案,请参阅下面的链接文章。

    如何使用XmlReader解析XML
  • 如何解析XML文件?

使用更好的方法来解析你的数据将使你需要解决的问题更容易

注意,有很多关于这个主题的文章-搜索谷歌"阅读xml c#"

HTH,