Forum > General

read data files

(1/5) > >>

dicas3d:
procedure TfrmLogin.Button1Click(Sender: TObject);
Type arquivo = RECORD
nome : string;
password : string;
end;
login = file of arquivo;
var f : login;
conta : arquivo;
c, nome, pass : string;
i, d : integer;
begin
nome := Edit1.Caption;
pass:= edit2.Caption;
c := envstr(2)+ '\contas.sgs';
system.Delete(c,1,4);
system.Assign(f,c);
system.Reset(f);
while not eof(f) do read(f,conta);
system.close(f);
end;
when i am at then end of execution it shows that error:
Project name raised exception class 'External:SIGSEGV'.

theo:
What's the purpose of this poll?

To your question: I don't know because I never use pascal-style file access.

But I can tell you, that it is a bad idea to declare sth. as "string" in records.
String depends on the setting $H+ / $H-. In mode $H+, String is a reference counted "AnsiString".
In this mode, the record holds (and saves) only a pointer to an AnsiString.
Better use ShortString or String[NumberOfChars]

dicas3d:
It works.

dicas3d:
It works but no well.
No is that code:

--- Quote ---procedure TfrmLogin.Button1Click(Sender: TObject);
Type login = RECORD
nome : shortstring;
password : shortstring;
end;
var f : file of login;
linha : array[0..1000] of login;
c, nome, pass : shortstring;
i, d : integer;
begin
nome := Edit1.Caption;
pass:= edit2.Caption;
c := envstr(2)+ '\contas.sgs';
system.Delete(c,1,4);
system.Assign(f,c);
system.Reset(f);
i := 0;
while not eof(f) do begin
read(f,linha);
i := i + 1;
end;
if linha.password = pass then begin
end;
system.close(f);
//Form1.show;
end;  
--- End quote ---
It don't read linha.password.

typo:
Try this code:


--- Code: ---procedure TForm1.Button2Click(Sender:TObject);
var
  f :textfile;
  linha: string;
  arquivo :string;
  OK :boolean;
begin
  OK := False;
  arquivo := 'contas.sgs';
  System.Assign(f, arquivo);
  System.Reset(f);
  try
    while not Eof(f) do
    begin
      Read(f,linha);
      // lines are written in format "login = password"
      if (Pos(Edit1.Text + ' ', linha) > 0) and (Pos('= '+ Edit2.Text, linha) > 0) then
      begin
        OK := True;
        Break;
      end;
    end;
  finally
    System.Close(f);
  end;
  if OK then showmessage('Login exists and Password is OK')
  else ShowMessage('Unknown Login or Password');
end;       
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version