
THE FILES (part 1)
Turbo Pascal provides a complete set of file management instructions. The FILE type is represented by a sequence of objects of the same type, the size of a file is not determined by the type, but a pointer is provided that moves along the file, for example when data is added, the pointer moves to the next, but it can also be managed by the program in order to retrieve the information sought.
Let's see an example of definition of FILE
Type
Names = string[30];
var
Numbers: file of integer;
names: file of names;
In the example we have defined two variables, the first numbers, which is an integer file and names which we have defined as string files. Once we have defined one or more file-type variables, let's see how to operate. Turbo pascal provides a certain number of procedures and functions with which to manipulate the file:
Considering the numbers file that we declared in the example as a file, the assignment, reading, writing, etc. procedures They are the following:
Assign (Numbers, FileName)
FileName is a string variable that indicates the name of the file, according to the standards allowed by the operating system. The name is associated with the file type variable and all read and write operations performed on the variable
will modify the file NameDelFile. For example
assign(Numbers,'Prod.txt');
Uniquely assign the Numbers variable to the 'Prod.txt' file on disk.
Rewrite(Numbers, dim);
If the file assigned with Assign exists, it is reinitialized, the contents are lost, and the file pointer is set to zero.
Reset(Numbers,dim);
If the open file does not exist, an error is generated, otherwise the pointer is placed at the beginning of the file, but the content is preserved.
The Rewrite and Reset procedures have an optional second parameter (dim) that can be used with untyped files and allows you to specify the number of bytes per block. For example, you can specify a block size of 1 byte and perform a BlockRead of 128 blocks and get the same result as reading a block of 128 bytes. The advantage of this second parameter is that the program no longer has to handle truncation or overflow of a file because the length of the file is not divisible by 128.
Read(Numbers,data);
the information read from the file is assigned to one or more data variables (separated by commas), which must be of the type indicated in the declaration. Once the information has been read, the pointer is moved to the next value.
Write(Numbers,data);
the information contained in the data variable(s) (separated by comma) is written to the file and the pointer is moved to the next position
Seek(Numbers,n);
Seek moves the file pointer to the nth component of the file. n is an integer type expression. The position of the first element of a file is 0. You can use a value for n that is greater than the length of the file to write past the last element of a file. The seek(Numbers, FileSize(Numbers); statement then places the file pointer at the end of the file (FileSize returns the number of components in the file, and as the components are numbered from zero, the number returned is greater than one
Flush(Numbers);
Flush empties the internal buffer of the Numbers file and thus ensures that the sector buffer is actually written to the disk in case of write operations since the last disk update. Flush also ensures that the next read operation will actually perform a physical read from the file on disk. Flush should never be used on a closed file.
Close(Numbers);
Closes the Numbers file and updates the disk contents. It is always advisable to close a file after use so as not to keep too many pointers occupied
Erase(Numbers);
rename(Numbers,NewFileName);
Rename the Numbers file, with the name contained in the string variable, NewFileName. Then all operations will be performed on the new file. Rename must be used on an open file. It is the programmer's job to ensure that NovoFileName does not already exist on disk
A series of standard functions that operate on files are then provided:
Eof(Numbers);
Boolean function that returns True if the file pointer is positioned at the end of the file on disk, i.e. beyond the last component of the file. otherwise EOF returns False.
FilePos(Numbers);
returns an integer value indicating the position of the pointer, if at the beginning of the file, returns 0
File Size(Numbers);
returns an integer value indicating the size of the file expressed as the number of data that make up a file.
A special mention is necessary for text files that are declared with the Text type. Text files have the particularity of using the same instructions that are used to write text on the screen, with the difference that the destination file must be indicated. For example, to print a string constant on the screen we use the instruction Write 0 Writeln if we want a carriage return after printing, in this way:
Write('Hello World!');
or
Writeln('Hello World!');
to write the same information in a text file, simply declare a text file and initialize it, in the same way we saw before:
Var TextFile text: Text {we declare a file of type text}
…
Assign (TextFile,'text.txt'); {we assign the file the disk name 'testo.txt'}
At this point to write to the file we will simply write using the Write or Writeln keywords passing as a parameter the file identifier before the data to be written:
Write(TextFile,'Hello World!');
or
Writeln(TextFile,'Hello World!');
The file we obtain will be logically organized into lines of text terminated with the CR LF characters (asci 13 and ascii 10) or carriage return and line feed.
Naturally, a file of this type can be opened and modified like any other text file.

0 Comments