2012-12-07 39 views
0

隨着下面的代碼我寫一個的Delphi:塊寫入I/O錯誤1784

dest : array of Bytes; 

到文件中。

c: integer; 
    size: integer; 

如果我去做了逐字節:

filename := ExePath + 'test.txt'; 
    AssignFile(myfile, filename); 
    ReWrite(myfile, 1); 
    Write the data array to the file 
    for c := 0 to length(dest) - 1 do 
    BlockWrite(myfile, dest[c], 1); 
    CloseFile(myfile); 

一切工作正常,但需要大陣列(最大20MB)年齡。

如果我嘗試寫@一旦我得到的I/O錯誤1784:

filename := ExePath + 'test.txt'; 
    AssignFile(myfile, filename); 
    size := length(dest); 
    ReWrite(myfile, size); 
    BlockWrite(myfile, dest[0], size); 
    CloseFile(myfile); 

哪裏可能的錯嗎? 在此先感謝。

+1

恕我直言txt不是最好的exten除非你在一個字節數組中存儲文本,這不是最好的容器。 – jachguate

+0

請發佈您收到的確切錯誤消息。 '1784'是操作系統錯誤,而不是I/O錯誤(請參閱http://docwiki.embarcadero.com/RADStudio/XE3/en/Delphi_Runtime_Errors)。如果你向我們展示了'ExePath'的位置以及'myfile'是如何聲明的,那將會很有幫助。 –

+0

什麼是myfile的sdatatype(聲明)? –

回答

2

明白了...

@我:RTFM

BlockWrite(myfile, dest[0], size); 

必須

BlockWrite(myfile, dest[0], 1); 

事業規模已定義與重寫數組的大小....

filename := ExePath + 'test.txt'; 
AssignFile(myfile, filename); 
size := length(dest); 
ReWrite(myfile, size); 
BlockWrite(myfile, dest[0], 1); <-- 1 "dataset" of length (size) as defined before 
CloseFile(myfile); 
+0

編號 如果'myfile'是無類型文件,那麼通過'rewrite'調用指定的單元大小爲1個字節。如果'myFile'是鍵入的或者是文本文件 - 那麼比'rewrite'調用沒有意義,但是因爲我們看不到'myFile'聲明,所以我們不知道單元大小是多少。 http://www.freepascal.org/docs-html/rtl/system/rewrite.html http://wiki.freepascal.org/File_Handling_In_Pascal –

+2

@ Arioch'The他正在寫一個字節數組,他爲什麼要他使用類型文件? :o) – whosrdaddy

+0

@whosrdaddy他已經寫道,他完全忘了所有那些沒有RTFMing的東西。所以他只能盲目地從一些物體或文章中複製宣言。雖然我有一種感覺,調用類型化文件的二進制ReWrite將是語法錯誤,並不會編譯。或者至少在TP時代已經迴歸了。 –