There is a problem to work with the file. I create a file of type integer. I write down there 10 numbers, but when opening through a notebook - there are other characters. Tell me, please, what to add for correct display. Here is a simple file entry:

const n=10; var a:array[1..n] of integer; f:file of integer; i,min,o,sum:integer; begin randomize; assign(f, 'file1.txt'); rewrite(f); for i:=1 to n do begin a[i]:=random(20); write(f, a[i]); end; close(f); end. 

    1 answer 1

    You write numbers to the file of type integer , that is, in fact it is a binary file. A text file consists of characters. When you open a file through a notebook, it displays the character codes of the numbers you entered, and the characters with an ASCII code less than 32 are unreadable. To enter the numbers you need displayed

    1. Change file format to Text

      f: Text;

    2. convert number to string with Str

    To do this, add the variable S: string to the Var section.

    and before doing the conversion do

    Instead of write(f ,a[i]); :

     Str(a[i], S); Write(f, S + ' '); 

    For readability add a space. Then the file will be readable, but if you conduct further processing, it is better to leave the file in integer format, since it is more convenient to use the binary format when processing the file. You immediately read the numbers in the native format without conversion.

    It all depends on the tasks.

    To convert a string to a number, use the Val function.

    The chr function does not work in this case, because the file format is not file of char .

    • one
      The write procedure itself is perfectly able to print integers, so there is no need to call str . In other words, it suffices to call write(f, a[i]); , but only the file should be text, as mentioned above, and not file of integer . - Ilya