0

With following code i write an

  dest : array of Bytes;

to a file.

  c: integer;
  size: integer;

If i do it Byte by Byte:

  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);

everything works fine, but takes ages on large arrays (20MB biggest).

If i try to write it @ once i get I/O Error 1784:

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

Where is may fault? Thanks in advance.

Michael Grenzer
  • 503
  • 1
  • 9
  • 20
  • 1
    IMHO txt is not the best extension for a binary file, unless you're storing text in an array of bytes, which is not the best container for that. – jachguate Dec 07 '12 at 02:01
  • Please post the exact error message you receive. `1784` is an operating system error, not an I/O error (see http://docwiki.embarcadero.com/RADStudio/XE3/en/Delphi_Runtime_Errors). It would be helpful if you showed us where `ExePath` is located as well, and how `myfile` is declared. – Ken White Dec 07 '12 at 02:03
  • what is the sdatatype (declaration) of `myfile` ? – Arioch 'The Dec 07 '12 at 07:07

1 Answers1

3

Got it...

@ myself: RTFM

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

must be

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

cause size is defined already to the size of the array with rewrite....

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);
Michael Grenzer
  • 503
  • 1
  • 9
  • 20
  • No. If `myfile` is untyped file, then the specified cell size is 1 byte by `rewrite` call. If `myFile` is typed or text file - then than `rewrite` call had no sense, but as we saw not `myFile` declaration we cannot know what is the cell size. http://www.freepascal.org/docs-html/rtl/system/rewrite.html http://wiki.freepascal.org/File_Handling_In_Pascal – Arioch 'The Dec 07 '12 at 07:11
  • 2
    @Arioch'The he's is writing an array of bytes, why on earth would he be using a type file? :o) – whosrdaddy Dec 07 '12 at 07:59
  • @whosrdaddy he already wrote that he totally forgot all that stuff without RTFMing. So he could just have the declaration copied blindly from some object or article blindly. Though i have a feeling that calling binary ReWrite over typed file would be syntax error and would not compile. Or at least was so back in TP times. – Arioch 'The Dec 07 '12 at 10:08
  • @whosrdayddy compare: "i dump down an array of TMySuperRecord. Would `file of TMySuperRecord` be of any help to me ? probably not... How could it be?" :-) "array" is just array. Maybe his data are 12-bytes chunks or whatever there might happen. Well, anyway issue is solved and no reason to continue it. – Arioch 'The Dec 07 '12 at 10:11