Writeln Locking in Delphi Seattle

Asked

Viewed 498 times

3

I’m having a problem where by going through the job write it ends up locking inside the function, and does not return anything, having to close the application and open again, below the code for testing, just create a project and paste the code into Formcreate:

procedure TForm1.FormCreate(Sender: TObject);
var
  F        : TextFile;
  mArquivo,s : String;
  i: integer;
begin
   {$i+}
   mArquivo :=  'C:\Users\Rodrigo\Desktop\teste.txt';
   AssignFile(F,mArquivo);

   if FileExists(mArquivo) then
      DeleteFile(mArquivo);

   Rewrite(F,mArquivo);
   Append(F);

   for i := 0 to 10 do
   begin
      S := 'Text'+ IntToStr(i);
      Write(F,S);
   end;
   CloseFile(F);
end;
  • 1

    Buddy, I don’t have the Seatle, and yes the XE 7, but what are the parameters requested by the function Rewrite()? Because trying to play your code, I just passed the variable F in function Rewrite and everything worked out correctly.

  • Now that you spoke, it worked even taking away the argument that was there, to tell the truth had not realized this field and it worked.

2 answers

6

Commented on the code found.

procedure TForm1.FormCreate(Sender: TObject);
var
  F        : TextFile;
  mArquivo,s : String;
  i: integer;
begin
   {$i+}
   mArquivo :=  'C:\Users\Rodrigo\Desktop\teste.txt';
   AssignFile(F,mArquivo);

   // Se você vai usar o rewrite abaixo então não tem
   // sentido fazer isso aqui.   
   if FileExists(mArquivo) then
      DeleteFile(mArquivo);

   // O rewrite cria o arquivo e, caso ele exista, vai apaga-lo
   // e recriar um novo.

   Rewrite(F,mArquivo); // Erro

   // Append serve para você abrir um arquivo e adicionar novas linhas a  
   // a ele. Aqui é totalmente desnecessário. 
   Append(F);

   for i := 0 to 10 do
   begin
      S := 'Text'+ IntToStr(i);
      // Isso, claro, deveria ser WriteLn
      Write(F,S);
   end;
   CloseFile(F);
end;

Rewriting.

procedure TForm1.FormCreate(Sender: TObject);
var
  F        : TextFile;
  mArquivo,s : String;
  i: integer;
begin
   {$i+}
   mArquivo :=  'C:\Users\Rodrigo\Desktop\teste.txt';
   AssignFile(F,mArquivo);

   // Como a variavel F é um textFile então rewrite tem apenas um parametro
   Rewrite(F);

   for i := 0 to 10 do
   begin
      S := 'Text'+ IntToStr(i);
      WriteLn(F,S);
   end;
   CloseFile(F);
end;

2


As commented, simply remove the parameter mArchive of function Rewrite and it will work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.