Generate Code text file done in Pascal

Asked

Viewed 923 times

2

I have a code in Pascal. Here’s the thing:

Program Pzim ;
   var
     i:integer;
     vect:array[1..1001] of integer;
Begin
     i:=1;
     for i:= 1 to 999 do
     vect[i]:=i+1;
     for i:= 1 to 999 do
     writeln (vect[i]);
   readln;
End.

The code in Pascalzim prints on the screen a sequence of numbers. I wanted to save in text file what is generated by the code.

would be possible using the same pascal, or who knows otherwise, using the Notepad++ for example?

2 answers

3


I found out! my question was answered on Stackoverflow

The code went like this:

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.

2

A small improvement that brings a learning regarding the organization of the impression of the result.

Program Pascal ;

var
  i, j:integer;
  vect:array[1..1000] of integer;
  Myfile: text;

begin
  j := 0;
  for i := 1 to 1000 do
  begin
    vect[i] := j + 1;
        j := j + 1;
  end;

  Assign(Myfile, 'Myfile.txt');
  Rewrite(MyFile);

  for i := 1 to 1000 do
  begin
    if i < 1000 then
        begin   
        Write(vect[i], ', ');
        Write(Myfile, vect[i], ', ');
    end else begin
        Write(vect[i], '. ');
        Write(Myfile, vect[i], '.');
    end;
  end;

  Close(Myfile);
  ReadLn;
end.

Browser other questions tagged

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