2016-12-01 8 views
0

그래서 난 내 프로그램은 텍스트 파일에서 읽고과 같이 명령 프롬프트 내부의 텍스트 파일에 어떤 표시가하려고 해요 ...C# 처리되지 않은 예외 : 닫힌 TextReader를 읽을 수 없습니다

enter image description here

그러나

다음 프로그램은 파일의 한 라인을 읽을 수 있습니다, 그리고 나는 그들이 불평 내 세 개의 .cs 파일의 라인을 통해려고 시도했습니다이 오류 예외 enter image description here

가 발생합니다. 디버깅과 코드 편집을 시도했지만, 더 많이할수록 더 나 빠졌고, 그래서 나는 매우 혼란스럽고 혼란스러워서 솔직히이 시점에서 무엇을 고쳐야할지 모릅니다.

이들은 불만을 표시하는 코드 줄이며 실제 코드 파일은 바로 아래에 있습니다.

FileReader.cs의 42 행 --->
return streamReader.ReadLine();

FileReader.cs :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 

namespace homework2 
{ 
    #region File Reader Class 
    // ___________________// 
    //  FILE READER // 
    //____________________// 
    class FileReader : Reader 
    { 
     private StreamReader streamReader; 

     #region File Reader File Accessing Method 
     // Accessing the file 
     public FileReader(string fileName) 
     { 
      streamReader = System.IO.File.OpenText(fileName); 
      if (streamReader == null) 
       throw new Exception("OpenRead() failed for file " + fileName); 
     } 
     #endregion 

     #region Read Method 
     // Read Method 
     public override string Read() 
     { 
      // get and return a single line of text 
      return streamReader.ReadLine(); 
     } 
     #endregion 

     #region Close Method 
     // Close Method 
     public override void Close() 
     { 
      streamReader.Close(); 
     } 
     #endregion 
    } 
    #endregion 
} 

라인 MorgReader.cs --->
동안의 48 ((박스 = Wrapped.Read()) == NULL)

MorgReader .cs :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using homework2; 

namespace homework2 
{ 
    #region Morg Reader Class 
    //___________________// 
    // MORG READER // 
    //___________________// 
    class MorgReader : ReaderDecorator 
    { 
     MorgFactory Fact; 

     public MorgReader(MorgFactory fact, Reader wrapped) : base(wrapped) 
     { 
      Fact = fact; 
     } 

     public override string Read() 
     { 
      return Wrapped.Read(); 
     } 

     public override void Close() 
     { 
      Wrapped.Close(); 
     } 

     public Morg ReadMorg() 
     { 
      #region splitting the text string 
      // A way to organize the block of text 
      string Box = " "; 
      while ((Box = Wrapped.Read()) == null) 
       return null; 
      string[] Part = Box.Split(','); 
      #endregion 


      #region Displaying each line of text from Morg Reader File to Morg factory 
      // Translate the info from Morg file to Morg factory to the program 
      Morg FactMorg = Fact.CreateMorg(); 
      FactMorg.setName(Part[0]); 

      #region Converting location string to its variable 
      // A way to convert the string location to the current variable for location 
      Location FactLoc; 
      FactLoc = new Location(); 
      FactLoc.X = Convert.ToInt32(Part[1]); 
      FactLoc.Y = Convert.ToInt32(Part[2]); 
      #endregion 

      /* FactMorg.Move(FactLoc.X, FactLoc.Y); */ 
      FactMorg.setLocation(FactLoc); 
      FactMorg.setMovement(Part[3]); 
      FactMorg.setFeeding(Part[4]); 

      #endregion 

      return FactMorg; 
     } 
    } 
    #endregion 
} 

라인 Program.cs --- 33>
동안 (! (NewMorg = MyReader.ReadMorg()) = NULL)

Program.cs :

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 

namespace homework2 
{ 
    #region Main Program 
    class Program 
    { 
     static void Main(string[] args) 
     // ____________________ // 
     //  MAIN PROGRAM  // 
     //______________________// 
     { 

      MorgReader MyReader = (new MorgReader(new MyMorgFactory(),new FileReader("morgs.txt"))); 

      Morg NewMorg = new Morg(""); 

      while ((NewMorg = MyReader.ReadMorg()) != null) 
      { 

       string NewMorgName = NewMorg.getName(); 
       Location NewMorgXY = NewMorg.getLocation(); 
       string NewMorgMovement = NewMorg.getMovement(); 
       string NewMorgFeeding = NewMorg.getFeeding(); 

       Console.WriteLine(NewMorgName + " " + NewMorgXY.X + " " + NewMorgXY.Y + " " + NewMorgMovement + " " + NewMorgFeeding); 

       MyReader.Close(); 

      } 

      //MorgTypeA Morg1 = new MorgTypeA("Axel, the Axe-shaped Morg"); 
      //MorgTypeB Morg2 = new MorgTypeB("Bael, the Dark Morg"); 
      //MorgTypeC Morg3 = new MorgTypeC("Corona, the Light Morg"); 

      //Simulator morgGame = new Simulator(Morg1, Morg2, Morg3); 

      //morgGame.run(); 
     } 
    } 
    #endregion 
} 

이 내 최초의 C#을하고 나는이 아주 새로운 해요. 당신의 큰 도움에 감사드립니다.

답변

2

while 루프의 첫 번째 반복에서 독자를 종료하고 있습니다.

public static void Main(string[] args)  
{ 
    //some stuff 

    while ((NewMorg = MyReader.ReadMorg()) != null) 
    { 

     string NewMorgName = NewMorg.getName(); 
     Location NewMorgXY = NewMorg.getLocation(); 
     string NewMorgMovement = NewMorg.getMovement(); 
     string NewMorgFeeding = NewMorg.getFeeding(); 

     Console.WriteLine(NewMorgName + " " + NewMorgXY.X + " " + NewMorgXY.Y + " " + NewMorgMovement + " " + NewMorgFeeding); 

     //MyReader.Close(); -> this line put it out from the while loop 

    } 

    //put it here 
    MyReader.Close(); 

    //other stuff 

} 
+1

예! 정말 고맙습니다! – miiworld2