Previous topic | Ada Home Page | Index

Using text files - Example


------------------------------------------------------------
-- copy one file to another
-- the user specifies the names of the two files
--
-- Skansholm, pages 416-417
------------------------------------------------------------

with TEXT_IO; use TEXT_IO;

procedure copy_file is

    inf         : TEXT_IO.FILE_TYPE;   -- input file
    outf        : TEXT_IO.FILE_TYPE;   -- output file
    line        : string (1..200);     -- one line of a file
    line_length : NATURAL;             -- line length
    file_name   : string (1..30);      -- name of a file
    name_length : natural;             -- name length

begin

    -- open the input file
    PUT_LINE ("Enter name of file to be copied");
    GET_LINE (file_name, name_length);
    OPEN     (inf, IN_FILE, file_name(1..name_length));

    -- open the output file
    PUT_LINE ("Enter the name of the copy");
    GET_LINE (file_name, name_length);
    CREATE   (outf, file_name (1..name_length));

    -- copy inf to outf
    while not END_OF_FILE (inf) loop
        GET_LINE (inf,  line, line_length);
        PUT_LINE (outf, line (1..line_length);
    end loop;

    -- close the files
    CLOSE (inf);
    CLOSE (outf);

end copy_file;


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 23 Feb 96