Add to Text File Using Pascal

1

I made this algorithm in Pascal which feeds numeric 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, as soon as this text file was fed, the program would automatically add the current date to each line that was inserted.

I'm not asking for the code or the program already worked out. Just an idea how to use Assign , For example, or maybe using a .bat file. I do not know.

I am learning programming logic but I am not aware of some tools of each language, such as an interaction between files or programs.

    
asked by anonymous 15.02.2016 / 15:30

2 answers

2

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

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

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

    
16.02.2016 / 14:24
1

I think 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 / Mar / 2016.

1000 - 0:18:42 - Sbado, 26 / Mar / 2016.

The delay was only included to give a space of time between the records so as to verify that the time was set correctly.

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.
    
26.03.2016 / 03:55