Files need to be:
An example program shows how the contents of one file can be copied to another. The program includes all of the elements listed above and described below.
with TEXT_IO; ... filevar : TEXT_IO.FILE_TYPE;
Commonly two file variables
Open an existing file for input:
OPEN (FILE => filein, MODE => IN_FILE, NAME => "oldstu.dat"); OPEN (filein, IN_FILE, "oldstu.dat");
CREATE (FILE => fileout, MODE => OUT_FILE, NAME => "newstu.dat"); CREATE (fileout, "newstu.dat");
Open an existing file for output (over-write its contents):
OPEN (FILE => fileout, MODE => OUT_FILE, NAME => "newstu.dat"); OPEN (fileout, OUT_FILE, "newstu.dat");
Sometimes a program needs to process a file twice
You could always close the file, and then reopen it, but Ada provides a shortcut for this situation.
RESET procedure:
Example: read file twice
OPEN (filevar, IN_FILE, filename); ... code to read from the file ... RESET (filevar); ... code to read the file all over again ... CLOSE (filevar);
Example: write, then read
CREATE (filevar, OUT_FILE, filename); ... code to write to the file ... RESET (filevar, IN_FILE); ... read the information just written ... CLOSE (filevar);
PUT, GET etc may have a file variable as first argument
PUT ("message"); -- to screen PUT (fileout, "message"); -- to file SET_COL (fileout, 5); PUT (fileout, "message");
CLOSE (filevar);
Ada lets you treat the terminal as a text file (or files).
GET (num); GET (STANDARD_INPUT, num);
PUT ("message"); PUT (STANDARD_OUTPUT, "message");