Pastebin

Paste #2475: pascal write, runerror 1784

< previous paste - next paste>

Pasted by Anonymous Coward

Download View as text

procedure WriteFile(Filename: String; BufferSize: Integer);
var
   //Buffer: Array of Byte;
   Buffer: PByte;
   BytesWritten, i, j: Int64;
   StartTime, EndTime, TmpTime, TmpTime2, Elapsed: integer;
   WriteTime, WriteSpeed: Double;
   StatusLine : String;
   OutFile: File;
   tmp: Word;
begin
  Log('WriteFile("'+Filename+'", '+IntToStr(BufferSize)+')');
  StartTime := GetTickCount();
  BytesWritten:=0;
  //SetLength(Buffer, BufferSize);

  Buffer := AllocMem(SizeOf(Byte) * BufferSize);
  // Initialize buffer with random bytes
  Randomize;
  Log('Initializing buffer with random bytes...');
  for j:=0 to BufferSize do
  begin
    Buffer[j] := Random(256);
  end;
  Log('Initialized buf[0..' + IntToStr(j) + ']');

  // Write the file
  Log('Opening "' + Filename + '" for writing...');
  Assign(OutFile, Filename);
  Rewrite(OutFile, BufferSize);
  Log('File opened...');
  try
    Form1.ProgressBar1.Position:=0;
    Form1.ProgressBar1.Max:=Round((DiskFree(0) - BufferSize * 2 ) / 1024 / 1024);
    i := 0;
    While ( (doWrite = True ) and (DiskFree(0) > BufferSize * 2) ) do
    Begin
      Inc(i);
      If doWrite Then
      Begin
        TmpTime := GetTickCount64();
        BlockWrite(OutFile, Buffer, 1, tmp);
        FlushThread;
//        FlushFileBuffers(OutFile);
        BytesWritten:= BytesWritten + BufferSize;
        Elapsed := GetTickCount() - StartTime;

        Form1.StatusBar1.Panels[0].Text := 'Time elapsed: ' + IntToStr(Round(Elapsed/1024)) + ' s';
        Form1.StatusBar1.Panels[1].Text := 'MB written: ' + IntToStr(Round(BytesWritten/1024/1024));
        try
          Form1.StatusBar1.Panels[2].Text := 'Speed: ' + FormatFloat('0.00', (byteswritten / 1024/1024) / (elapsed / 1000)) + ' mb/s';
        Except
          on e: EZeroDivide do
          Form1.StatusBar1.Panels[2].Text := 'Speed: ' + 'NaN' + ' mb/s';
          on e: EDivByZero do
          Form1.StatusBar1.Panels[2].Text := 'Speed: ' + 'NaN' + ' mb/s';
        end;

        Form1.StatusBar1.Panels[3].Text := 'Free space: ' + FormatFloat('0.00', (DiskFree(0) / 1024/1024)) + ' mb';
        form1.ProgressBar1.Position:=Round(BytesWritten / 1024 / 1024);
        tmptime2 := GetTickCount64();

        StatusLine:='';
        StatusLine:=StatusLine + ' I='           + IntToStr(i);
        StatusLine:=StatusLine + ' BytesWritten='+ IntToStr(BytesWritten);
        StatusLine:=StatusLine + ' CycleTime='   + IntToStr(tmptime2 - tmptime);
        Log(StatusLine);

        Application.ProcessMessages;
      end
      else
      begin
        Log('Aborting!');
      end;
      Application.ProcessMessages;
    end;
    Log('Done!');
  finally
    // Close the file handle
    Log('Closing file handle.');
    Close(OutFile);
    //Free;
  end;
  EndTime:=GetTickCount();
  WriteTime:=(endtime-starttime)/1000;
  WriteSpeed:=BytesWritten/1024/1024/writetime;
  if doWrite then Form1.Memo1.Lines.Add(IntToStr(Round(BytesWritten/1024/1024)) + ' MB written in ' + FloatToStr(WriteTime) + ' seconds ('+ FloatToStr(WriteSpeed) +' MB/s) to ' + filename)
  else Log('Write aborted');
  Freemem(Buffer);
end;

New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.