Nugroho's blog.: [Delphi] Write to File (just for self documentation)

Wednesday, June 4, 2014

[Delphi] Write to File (just for self documentation)

Here the code, the input method is still manual though, :)



unit Unit1;

interface

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

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Button6: TButton;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Button7: TButton;
procedure proses;
procedure simpanfile;
procedure simpanrecord;
procedure bacarecord;
procedure simpan;
procedure tampilkan;
procedure awal;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
tmahasiswa = record
nama,nim,alamat,offering,nhuruf:string[30];
umur:integer;
nilai:real;
end;
var
Form1: TForm1;
fileku:textfile;
files: file of tmahasiswa;
mahasiswa: tmahasiswa;
implementation

{$R *.dfm}
procedure tform1.awal;
begin
edit1.Text:='Nama';
edit2.Text:='NIM';
edit3.Text:='18';{umur}
edit4.Text:='Alamat';
edit5.Text:='Offering';
edit6.Text:='81';{nilai}
edit7.Text:='Nilai Huruf';

button6.Caption:='Simpan';
button7.Caption:='Tampilkan';
end;
procedure tform1.simpan;
begin
assignfile(files,'data.mp4');
reset(files);
mahasiswa.nama:=edit1.Text;
mahasiswa.nim:=edit2.Text;
mahasiswa.umur:=strtoint(edit3.Text);
mahasiswa.alamat:=edit4.Text;
mahasiswa.offering:=edit5.Text;
mahasiswa.nilai:=strtofloat(edit6.Text);
mahasiswa.nhuruf:=edit7.Text;

seek(files,filesize(files));
write(files,mahasiswa);
closefile(files);
end;
procedure tform1.tampilkan;
begin memo1.Text:='';bacarecord;end;
procedure TForm1.Button6Click(Sender: TObject);
begin simpan; end;
procedure TForm1.Button7Click(Sender: TObject);
begin tampilkan;end;

procedure tform1.bacarecord;
var text:string;
begin
assignfile(files,'data.mp4');
//filemode:=fmopenread;
reset(files);
while not eof(files) do begin
read(files,mahasiswa);
text:=mahasiswa.nama+' ('+inttostr(mahasiswa.umur)+') '+mahasiswa.nim+' '+mahasiswa.alamat+' '+mahasiswa.offering+' '+floattostr(mahasiswa.nilai)+' ('+mahasiswa.nhuruf+')';
memo1.Lines.Append(text);
end;

closefile(files);

end;
procedure tform1.simpanrecord;
begin
assignfile(files,'data.mp4');
rewrite(files);

mahasiswa.nama:='string';
mahasiswa.nim:='001';
mahasiswa.alamat:='malang';
mahasiswa.offering:='abc';
mahasiswa.nhuruf:='wow+';
mahasiswa.umur:=20;
mahasiswa.nilai:=60;
write(files,mahasiswa);

mahasiswa.nama:='qwerty';
mahasiswa.nim:='002';
mahasiswa.alamat:='malang';
mahasiswa.offering:='abc';
mahasiswa.nhuruf:='wow';
mahasiswa.umur:=22;
mahasiswa.nilai:=55;
write(files,mahasiswa);

mahasiswa.alamat:='tidak di malang';
mahasiswa.offering:='abc';
mahasiswa.nhuruf:='wow-';
mahasiswa.umur:=18;
mahasiswa.nilai:=55;
write(files,mahasiswa);

mahasiswa.nama:='dvorak';
mahasiswa.nim:='004';
mahasiswa.alamat:='batu';
mahasiswa.offering:='nyasar';
write(files,mahasiswa);

closefile(files);

end;
procedure tform1.simpanfile;
var i:integer;
begin
assignfile(fileku,'filesaya.txt');
rewrite(fileku);

write(fileku,'Hello ');
write(fileku,'world');

writeln(fileku);
for i:=1 to 10 do begin
write(fileku, i/2, ' ' );
//writeln(fileku);
end;

writeln(fileku);

for i:=1 to 10 do begin
write(fileku, i/2:5:1, ' ' );
//writeln(fileku);
end;


closefile(fileku);


end;

procedure tform1.proses;
var i:integer;
begin
memo1.Text:='';
for i:=1 to 10 do begin
memo1.Lines.Append('OK, ini adalah baris ke-'+inttostr(i));
end;
memo1.Lines.Strings[5-1]:='dengan';
{}
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
proses;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
simpanfile;
end;

procedure TForm1.Button3Click(Sender: TObject);
var text:string;
begin
assignfile(fileku,'filesaya.txt');
reset(fileku);
while not eof(fileku) do begin
readln(fileku,text);
memo1.Lines.Append(text);
end;
closefile(fileku);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
simpanrecord;

end;

procedure TForm1.Button5Click(Sender: TObject);
begin
bacarecord;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
awal;
end;

end.

<\pre>

No comments:

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)