Handling of temporary text files

Asked

Viewed 36 times

1

I am trying to manipulate a text file on my system, in it will have to contain the name of the machine that the system is running. Only I can’t write in that file.

Ps: The purpose of this file is not to save the text, it is to have the text in it until the system is closed.

Below I will leave the code of how I create and try to write in the file, and only in the output of the system I close the file.

// Aqui o Autosad cria os arquivos de Controle de Usuário
for I := 1 to QuantUsuarios do
begin
  NomeArq := DM.tbParamPASTA_BD.AsString+'\Controle'+IntToStr(I)+'.txt';
  // Verificando se o arquivo existe
  if not FileExists(nomeArq) then
  begin
    NomeMaquina := 'Controle: '+CompName; // CompName é uma função que pega o nome da maquina.
    AssignFile(Arq, NomeArq); 
    Rewrite(Arq);             
    Writeln(Arq,NomeMaquina); 
    Break;
  end;

  if I >= QuantUsuarios then
  begin
    ShowMessage('Erro ao tentar Criar o arquivo de Controle!'+#13+'Acione o SUPORTE!!');
    // Encerra o Sistema
    DMl.db_AutoSad.Connected := False;
    frmEntradaSistema.close;
  end;
end;

this code I adapted from several websites I researched. if you have any other way of doing.

  • 2

    If you don’t want to save the file, just keep the data while running the program, you don’t need to create a file. You can keep the data, for example, in a Strignglist.

1 answer

1


Opa Alright!
Probably you cannot see the contents of the file because it is not saved.

Old man you can do so, after Writeln(Arq,NomeMaquina); you close the file, it will save the text on disk. then reopen the file with Reset(Arq);. There you will be able to view what is inside the file and or modify if it is your will there at the time you close your system you delete.

Reset(); Prepares an existing file for reading and puts the pointer file at the beginning. If the file does not exist, this instruction will cause an Input/Output error (Input/Output).

// Verificando se o arquivo existe
if not FileExists(nomeArq) then
begin
  AssignFile(Arq, NomeArq);
  Rewrite(Arq);
  Writeln(Arq, CompName);
  CloseFile(Arq);
  AssignFile(Arq, NomeArq);
  Reset(Arq);
  Break;
end;

Browser other questions tagged

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