Link to home
Start Free TrialLog in
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
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

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

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

Avatar of pr2501
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
you are asking a second question, which differs from the original ?

or am i mistaken ?
Avatar of 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
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pr2501

ASKER

Thank You, i appreciate your understanding.