Add to Text File Using pascal

Asked

Viewed 229 times

1

I did this algorithm on Pascal that feeds numerical information into a text file.

program Pascal; 
var
  i:integer;
  Vect:array[1..1001] of integer; 
  Myfile: text;
begin 
  i:=1;
  for i:=1 to 999 do 
    vect[i]:=i+1; 
  Assign(Myfile, 'Myfile.txt');
  Rewrite(MyFile);
  for i:=1 to 999 do
  begin 
    WriteLn(vect[i]);
    WriteLn(Myfile, vect[i]); 
  End;
  Close(Myfile); 
  ReadLn;
End.

I needed an idea for another program that, once this text file was fed, automatically the program would add the current date for each row that was inserted.

I’m not asking for the code or the program already prepared. Just an idea of how to use the Assign, For example, or maybe using a file .bat. I don’t know.

I am learning programming logic but I have no knowledge of some tools of each language, as in this case, an interaction between files or programs.

  • You want each number to be inserted next to the time it is being inserted?

  • That. Every N characters or N lines (whatever) the date and time

2 answers

2


In order for every element to be printed the date together, you will need to concatenate (join the text). In the pascal this is done with the +.

To enter the date you can use DateTimeToStr(Now).

To run something every X iterations, you can use the operator mod. He is the operator of rest of a division. Using if (X mod Y = 0) means it will execute the code inside the if each Y iterations. (or rather, whenever X is a multiple of Y)

1

I believe the code below meets your demand, the Unit indicated by the companion would only meet your need if you work with freepascal, this code I made with pascalzim.

The output generated at the prompt and saved in the file follows the following format:

1 - 0:16:56 - S bado, 26/Marco/2016.

905 - 0:18:32 - S bado, 26/Marco/2016.

1000 - 0:18:42 - S bado, 26/March/2016.

The delay was included only to give a space of time between the records to thus be found that the time was correctly placed.

program ArquivoNumDate; 
const
    DiaDaSemana: array[0..6] of string ('Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado');
    MesDoAno: array[1..12] of string=('Janeiro','Fevereiro','Marco','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro');

var
   i:integer;
   Vect:array[0..1001] of integer; 
   Arquivo: text;
   dia, mes, ano, diaSemana: integer;
   hora, minuto, segundo, msegundo: integer;

begin       

   for i := 0 to 999 do 
       vect[i]:= i + 1; 

   Assign(Arquivo, 'Arquivo.txt');
   Rewrite(Arquivo);

   for i := 0 to 999 do
   begin           
      delay(100);
      GetDate(ano, mes, dia, diaSemana);
      GetTime(hora, minuto, segundo, msegundo);   
      WriteLn(vect[i],' - ', hora,':', minuto,':', segundo, ' - ', DiaDaSemana[diaSemana],', ',dia,'/',MesDoAno[mes],'/',ano,'.');
      WriteLn(Arquivo, vect[i],' - ', hora,':', minuto,':', segundo, ' - ', DiaDaSemana[diaSemana],', ',dia,'/',MesDoAno[mes],'/',ano,'.');
   End;

   Close(Arquivo); 
   ReadLn;

End.

Browser other questions tagged

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