File input/output is part of Short Circuit's Console Program Basics selection.
Task

Create a file called   "output.txt",   and place in it the contents of the file   "input.txt",   via an intermediate variable.

In other words, your program will demonstrate:

   how to read from a file into a variable
   how to write a variable's contents into a file


Oneliners that skip the intermediate variable are of secondary interest — operating systems have copy commands for that.




import StdEnv
 
copyFile fromPath toPath world
    # (ok, fromFile, world) = fopen fromPath FReadData world
    | not ok = abort ("Cannot open " +++ fromPath +++ " for reading")
    # (ok, toFile, world) = fopen toPath FWriteData world
    | not ok = abort ("Cannot open " +++ toPath +++ " for writing")
    # (fromFile, toFile) = copyData 1024 fromFile toFile
    # (ok, world) = fclose fromFile world
    | not ok = abort ("Cannot close " +++ fromPath +++ " after reading")
    # (ok, world) = fclose toFile world
    | not ok = abort ("Cannot close " +++ toPath +++ " after writing")
    = world
where
    copyData bufferSize fromFile toFile
        # (buffer, fromFile) = freads fromFile bufferSize
        # toFile = fwrites buffer toFile
        | size buffer < bufferSize = (fromFile, toFile) // we're done
        = copyData bufferSize fromFile toFile // continue recursively