Avatar of pr2501
pr2501
 asked on

How to write "1" or "0" into *.txt file?

I need procedure which will  on base of one Variable write  "1"  or "0" into *.txt file.
The Variable must be checked constantly and when Variable changes it state, writing must be performed.

And windows browser dialog to define where file should be created.

thank You
Delphi

Avatar of undefined
Last Comment
pr2501

8/22/2022 - Mon
Sinisa Vuk

It's easy... You can move SaveDialog.Execute out of procedure and pass
destination file as parameter.

//defined in main class
OldValue: String;
...
//in class OnCreate event - set initial value
OldValue := '';
....
procedure TForm1.CheckState(chkVar: String);
var
  iFileHandle: Integer;
  pText: PChar;
begin
  if chkVar<>OldValue then
  begin
    OldValue := chkVar;
    if SaveDialog1.Execute then
    begin
        if FileExists(SaveDialog1.FileName) then
          iFileHandle := FileOpen(SaveDialog1.FileName, fmOpenReadWrite)
        else
          iFileHandle := FileCreate(SaveDialog1.FileName, fmOpenReadWrite);
        if iFileHandle>=0 then
        begin
          pText:=PChar(chkVar);
          FileWrite(iFileHandle, pText^, Length(chkVar));
          FlushFileBuffers(iFileHandle);
          FileClose(iFileHandle);
        end;
    end;
  end;
end;
....

Open in new window

Geert G

it would be much better if you use a property setter
>> you wouldn't need to continuously check the value, all changes can be logged (even the non-changes)
(if you are using a global variable ... you'll have to create a class around it.
so i assume you are using a class variable)

sample of how it could be now:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
  private
    aValue: string;
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  aValue := 'X';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  aValue := 'Y';
end;

end.

Open in new window


the change: use a property
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    faValue: string;
    fLogFileName: string;
    fFileLogging: Boolean;
    procedure SetaValue(const Value: string);
    procedure AddLog(OldValue, NewValue: string; Lines: TStrings  = nil);
  public
    property aValue: string read faValue write SetaValue;
    property LogFileName: string read fLogFileName write fLogFileName;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  aValue := 'X';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  aValue := 'Y';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  fLogFileName := ChangeFileExt(Application.ExeName, '.log');
  fFileLogging := False;
  faValue := '-';
end;

procedure TForm1.SetaValue(const Value: string);
begin
  if faValue <> Value then
  begin
    AddLog(faValue, Value, Memo1.Lines);
    faValue := Value;
  end;
end;

procedure TForm1.AddLog(OldValue, NewValue: string; Lines: TStrings  = nil);
var iFileHandle: integer;
  Temp: string;
  pText: PChar;
begin
  Temp := Format('%s Old: "%s" >> New: "%s"', [FormatDateTime('yyyymmdd hh:nn:ss.zzz', Now), OldValue, NewValue]);
  if FileExists(fLogFileName) then
    iFileHandle := FileOpen(fLogFileName, fmOpenReadWrite)
  else
    iFileHandle := FileCreate(fLogFileName, fmOpenReadWrite);
  if iFileHandle >= 0 then
  try
    pText := PChar(Temp);
    FileWrite(iFileHandle, pText^, Length(Temp));
    FlushFileBuffers(iFileHandle);
  finally
    FileClose(iFileHandle);
  end;
  if Assigned(Lines) then
    Lines.Add(Temp);
end;

end.

Open in new window

pr2501

ASKER
On create make file
C:\Program Files\EuroPlus\NiceLabel 4\Samples\Labels\myjob.job

with next text:

LABEL "C:\Program Files\EuroPlus\NiceLabel 4\Samples\Labels\mojtest.lbl"
PRINT 1
QUIT

when varaiable one changes state from 0 to 1
run
C:\Program Files\EuroPlus\NiceLabel 4\Samples\Labels\myjob.job
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Geert G

you are asking a second question, which differs from the original ?

or am i mistaken ?
pr2501

ASKER
Yes.  At first point i thought diferently because i had diferent  information what i need.

What must i do now?

It is that i have to open/run a
C:\Program Files\EuroPlus\NiceLabel 4\Samples\Labels\myjob.job
when state of one integer value changes from 0 to 1
ASKER CERTIFIED SOLUTION
Geert G

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pr2501

ASKER
Thank You, i appreciate your understanding.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.