Recent

Author Topic: File write problem: writeln vs TProcess CopyFrom(OutputStream, OutputStream.Size  (Read 1959 times)

Researching

  • Full Member
  • ***
  • Posts: 121
Thank you,
This file works.
If ran by Lazarus - error appears, but if ran under administrative mode - works fine.


AValue is "AddValue"??

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2009
  • Fifty shades of code.
    • Delphi & FreePascal
AValue is "a value", in my case, a string.
Show screenshot?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2009
  • Fifty shades of code.
    • Delphi & FreePascal
but if ran under administrative mode - works fine.
You can force that but then you will not be able anymore to run within Lazarus Debugger.
See the "execution Level"
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2009
  • Fifty shades of code.
    • Delphi & FreePascal
Thank you
You are welcome.

BTW, with my way of "how-to" you can append or create a new file, my "OpenMode" works as a selector for that behaviour.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Researching

  • Full Member
  • ***
  • Posts: 121
After some time of researching your code, have some questions left:
(actually could not catch how does this code work)
1. this code implements "FCreate", "FDelete"... How to write, rewrite, append in the same style?...
2. in the following block I would change the sequence of lines:
Code: Pascal  [Select][+][-]
  1. try
  2.       AddLog('Stream Handle is open for write.');
  3.       AddLog(FName_AValue + ' - created.');
  4.       fs.Position := 0;
  5.     finally            
to
Code: Pascal  [Select][+][-]
  1. try
  2.       fs.Position := 0;
  3.       AddLog('Stream Handle is open for write.');
  4.       AddLog(FName_AValue + ' - created.');
  5.     finally            
  6.  
And have no idea of which line makes a check like "if fileExists(FName....)"
3. This is first time I see a procedure declared inside procedure.
Code: Pascal  [Select][+][-]
  1. procedure AddLog(const AValue: string);
  2.   begin
  3.     Memo1.Lines.Add(AValue);
  4.   end;

Why the same procedure is implemented twice inside two procedures, instead of implementing separately and once??
Would you consider it right to learn assembly to be able to comprehend the debugger output.



« Last Edit: September 17, 2022, 05:45:45 pm by Researching »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2009
  • Fifty shades of code.
    • Delphi & FreePascal
Why the same procedure is implemented twice inside two procedures, instead of implementing separately and once??
because it was middle of night when i quickly typed all that :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2009
  • Fifty shades of code.
    • Delphi & FreePascal
everthing step by step explained
Code: Pascal  [Select][+][-]
  1. unit uMain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows,
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     SaveDialog1: TSaveDialog;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.   private
  22.     FFilename: string; // variable to store a destination
  23.     procedure BeginWriteTest; // a method to begin test writing
  24.     procedure AddLog(const AValue: string); // a method to pump a string into the memo1
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.FormCreate(Sender: TObject);
  39. begin
  40.   FFilename := ''; // reset internal variable
  41. end;
  42.  
  43. procedure TForm1.AddLog(const AValue: string);
  44. begin
  45.   Memo1.Lines.Add(AValue); // add a line to memo
  46. end;
  47.  
  48. // this is the handler for the button
  49. procedure TForm1.Button1Click(Sender: TObject);
  50. begin
  51.   Memo1.Clear; // clear the memo
  52.   FFilename := ''; // reset internal variable
  53.   AddLog('Open Save Dialog'); // write to log (memo1)
  54.   if SaveDialog1.Execute then // pop up the save file dialog
  55.     FFilename := SaveDialog1.FileName; // export the dialogs choosen destination to internal variable
  56.   if (FFilename <> '') then // when variable is not empty go on
  57.     BeginWriteTest // start the write method
  58.     else
  59.     AddLog('Open Save Dialog - canceled.'); // or tell that there was nothing to do
  60. end;
  61.  
  62. procedure TForm1.BeginWriteTest;
  63. const
  64.   OpenMode: array[Boolean] of Word = (fmCreate, fmOpenReadWrite); // my own way of how to open with stream a file
  65. var
  66.   fs: TStream; // my internal stream variable
  67.   b: boolean; // a boolean variable
  68. begin
  69.   AddLog('Destination: ' + FFilename);
  70.   AddLog('Inside Write Method.');
  71.   b := FileExists(FFilename, False); // test if a file exist or if we need to create a new
  72.   try
  73.     SetLastError(ERROR_SUCCESS); // reset any formerly error message
  74.     AddLog('Try to open a Stream Handle.');
  75.     fs := TFileStream.Create(FFilename, OpenMode[b] or fmShareCompat or fmShareExclusive, 0); // open a stream handle for writing, depend on fileexist how it is opened or created
  76.     try
  77.       AddLog('Stream Handle is open for write.');
  78.       AddLog(FFilename + ' - created.');
  79.       fs.Position := 0; // just to show how to access the stream
  80.     finally
  81.       fs.Free; // free the stream and close the handle
  82.       AddLog('Stream Handle is closed.');
  83.       if DeleteFile(FFilename) then // delete the file again to not leave trash on your drive
  84.         AddLog(FFilename + ' - deleted.')
  85.         else
  86.         AddLog(FFilename + ' - failed to deleted.');
  87.     end;
  88.   except
  89.     AddLog('Failed to open Stream Handle.');
  90.     AddLog('Message: ' + SysErrorMessage(GetLastError)); // on any exception do print the operating system message
  91.   end;
  92. end;
  93.  
  94. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018