File input/output: Difference between revisions

Added Clean example
(→‎[[E]]: forgot category)
(Added Clean example)
Line 272:
std::ostreambuf_iterator<char>(ofile));
}
 
==[[Clean]]==
[[Category:Clean]]
 
Define a function that copies the content from one file to another.
 
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
 
Apply this function to the world to copy a file.
 
Start world = copyFile "input.txt" "output.txt" world
 
==[[ColdFusion]]==
Anonymous user