Streamreader没有正确重置其位置 [英] Streamreader isn't reseting its location properly

查看:102
本文介绍了Streamreader没有正确重置其位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将streamreader的指针重置为文件的开头。我使用我的导师告诉我的代码但由于某种原因它将它重置到中间的某个地方。我不知道它指向哪里,但当我拉出一个消息框检查它读取所有数据,所以我不知道如何找出。



第一个按钮读取文件到列表框一次5行文本:

Im trying to reset the pointer of streamreader to the beginning of a file. Im using the code my instructor told me to but for some reason it resets it to somewhere in the middle. I don't know where its pointing to, but when i pull up a message box to check it reads all of the data so i don't know how to find out.

The first button reads to file out to a listbox 5 lines of text at a time:

private void btnPrint_Click(object sender, EventArgs e)
       {
           String DNA = "Line " + Line.ToString("000") + " = ";
           int Begin, end;
           Begin = 0;
           end = 5;
           while (Index < Lines && !DNAFile.EndOfStream)
           {
               DNA = "Line " + Line.ToString("000") + " = ";
               while (Begin < end)
               {
                   DNA += DNAFile.ReadLine() + " ";

                   Begin++;

               }
               end = end  + 5;
               Line++;
               lstOut.Items.Add(DNA);
               Index++;
           }
           DNAFile.DiscardBufferedData();
           DNAFile.BaseStream.Seek(0, SeekOrigin.Begin);
           DNAFile.BaseStream.Position = 0;

       }





第二个分析特定字符对并返回数字每组中每一个:



The second analyzes it for specific pairs of characters and returns the number of each in each group:

private void btnCountStartStop_Click(object sender, EventArgs e)
       {

           while (Index < Lines && !DNAFile.EndOfStream)
           {
               Codon = DNAFile.ReadLine();
               if (Codon == "ATG")
               {
                   Start++;
               }
               else if (Codon == "atg")
               {
                   Start++;
               }
               else if (Codon == "TAG")
               {
                   End++;
               }
               else if (Codon == "tag")
               {
                   End++;
               }
               else if (Codon == "TAA")
               {
                   End++;
               }
               else if (Codon == "taa")
               {
                   End++;
               }
               else if (Codon == "TGA")
               {
                   End++;
               }
               else if (Codon == "tga")
               {
                   End++;
               }
               Index++;

           }
           lstStats.Items.Add("Start Codons: " + Start);
           lstStats.Items.Add("Stop Codons: " + End);
           lstStats.Items.Add("----------------------------");

           DNAFile.DiscardBufferedData();
           DNAFile.BaseStream.Seek(0, SeekOrigin.Begin);
           DNAFile.BaseStream.Position = 0;


       }



当我首先运行每个按钮时,它会正确读取代码。第一个按钮将正确显示列表框中的文件内容。如果我先按下第二个按钮,它会读取15次启动和32次停止,这是正确的。但是,如果我将文本读到列表框然后计算密码子我只有10次开始和20次停止。如果我先进行计数并将文件读到第二个框,则不显示任何内容。我究竟做错了什么?提前谢谢



**更新**

这是课程级代码的开头


When i run each button first it reads the code properly. The first button will display the file contents in the listbox properly. And if i push the second button first it reads 15 starts and 32 stops which is correct. However if i read the text to the listbox then count the codons i only get 10 starts and 20 stops. If i do the counts first and read the file to the box second it displays nothing. What am i doing wrong? thanks in advance

**UPDATE**
this is the opening of the code at class level

public partial class frmDNA : Form
    {
        int Line = 1;
        int Start = 0;
        int End = 0;
        String Path, Codon;
        StreamReader DNAFile = new StreamReader("/documents/visual studio 2013/Projects/DNA/DNA.txt");
        int Index = 0;
        int Lines = File.ReadLines("/documents/visual studio 2013/Projects/DNA/DNA.txt").Count();

推荐答案

两者函数取决于调用它们时索引的当前值。一个函数增量但不是另一个。



另外,我们不知道在那些之外做了什么函数或文件内容。



无论如何,通过使用调试器或者添加一些跟踪来查看发生的情况,应该很容易找出问题。检查或输出位置以及其他变量可能会有很大帮助。对于第二个函数,它可以是:

Both functions depend on the current values of Index and Lines when they are called. One function increment Lines but not the other.

Also, we don't know what is done outside those functions or the file content.

Anyway, it should be very easy to figure out the problem by using a debugger or alternatively adding some trace to see what is happening. Inspecting or outputing the position along with other variables could help a lot. For the second function it could be:
var message = string.Format("{0} of {1}, B={2}, E={3}, C={4} [P={5}]",
    Index, Lines, Begin, End, Codon, DNAFile.BaseStream.Position);
Trace.WriteLine(message);



您可以在循环之前将该行放置一次,在每次迭代结束时放置一次。在每个函数的开头,你也可以输出函数名。



另一个技巧是从带有一行或两行的简化数据文件开始它用铅笔和纸手动。或者打印代码并在尝试时写入值。


You could place that line one time before the loop and one time at the end of each iteration. At the beginning of each function, you might also output the function name.

Another trick would be the start with a simplified data file with one or two lines and the try it manually with a pencil and paper. Or print the code and write the value as you try it.


这篇关于Streamreader没有正确重置其位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆