看板 C_Sharp 關於我們 聯絡資訊
在讀取檔案用readblock,可以固定讀出某個特定size。 但是我發現我遇到一個問題,我讀出來的size與檔案實際大小的size有差。 不知道為什麼會這樣。 希望版上的大大能撥空指點一下 謝謝 ~~ 例如我讀取test.doc 檔案大小是10752 bytes,但是我的程式讀取出來只有 8965 bytes。 不知道為什麼會這樣。 以下是我的程式碼: Stream filestream=null; StreamReader readAdapter=null; try { filestream=File.Open("test.doc",FileMode.Open,FileAccess.Read,FileShare.ReadWrite); } catch (FileNotFoundException e) { Console.WriteLine(e.ToString()); return; } readAdapter=new StreamReader(filestream); char []str=new char[1000]; int bytes=0; int total=0; do { bytes=readAdapter.ReadBlock(str,0,1000); Console.WriteLine("{0}",bytes); total+=bytes; } while(bytes==1000); Console.WriteLine("total:{0} bytes",total); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.131.51
virdust2003:沒試過,但我會改成這樣 while(bytes != 0) 03/12 13:37