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

pr2501
pr2501 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sinisa VukSoftware architect
Top Expert 2012

Commented:
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 GOracle dba
Top Expert 2009

Commented:
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

Author

Commented:
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
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Geert GOracle dba
Top Expert 2009

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

or am i mistaken ?

Author

Commented:
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
Oracle dba
Top Expert 2009
Commented:
technically you would have to ask a new question and close this one

sample extended:
creates files at startup
runs when going from '0' to '1'
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;
    fJobFile: string;
    fFileLogging: Boolean;
    procedure SetaValue(const Value: string);
    procedure AddLog(OldValue, NewValue: string; Lines: TStrings  = nil);
    procedure CreateJobFile(aFileName: string);
    procedure RunJob(aJobFile: string);
  public
    property aValue: string read faValue write SetaValue;
    property LogFileName: string read fLogFileName write fLogFileName;
  end;

var
  Form1: TForm1;

implementation

uses ShellApi;

{$R *.dfm}

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

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

procedure TForm1.CreateJobFile(aFileName: string);
var List: TStrings;
begin
  List := TStringList.Create;
  try
    List.Add('LABEL "C:\Program Files\EuroPlus\NiceLabel 4\Samples\Labels\mojtest.lbl"');
    List.Add('PRINT 1');
    List.Add('QUIT');
    List.SaveToFile(aFileName);
  finally
    List.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  fLogFileName := ChangeFileExt(Application.ExeName, '.log');
  fFileLogging := False;
  faValue := '-';
  fJobFile := 'C:\Program Files\EuroPlus\NiceLabel 4\Samples\Labels\myjob.job';
  CreateJobFile(fJobfile);
end;

procedure TForm1.RunJob(aJobFile: string);
begin
  ShellExecute(0, 'open', PChar(aJobFile), nil, nil, SW_SHOWNORMAL);
end;

procedure TForm1.SetaValue(const Value: string);
begin
  if faValue <> Value then
  begin
    AddLog(faValue, Value, Memo1.Lines);
    if (faValue = '0') and (Value = '1') then
      RunJob(fJobFile);
    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

Author

Commented:
Thank You, i appreciate your understanding.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial