Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (47 sloc) 1.13 KB

+++ title = "File input and output" date = 1970-01-01 [extra] task = """ Create a file called "output.txt", and place in it the contents of the file "input.txt" via an intermediate variable. """ [taxonomies] categories = ["task"] tags = ["basic", "cli"] languages = ["haskell", "rust"] +++

Haskell

Note: This doesn't keep the file in memory. Buffering is provided by lazy evaluation.

main = readFile "input.txt" >>= writeFile "output.txt"

Python

i = open("input.txt", "r")
o = open("output.txt", "w+")
o.write(i.read())

Rust

Note: The program will panic with any sort of error.

use std::fs::File;
use std::io::{Read, Write};

fn main() {
  let mut file = File::open("input.txt").unwrap();
  let mut data = Vec::new();
  file.read_to_end(&mut data).unwrap();
  let mut file = File::create("output.txt").unwrap();
  file.write_all(&data).unwrap();
}

JavaScript

const fs = require('fs');
const content = fs.readFileSync('input.txt');
fs.writeFileSync('output.txt', content);
You can’t perform that action at this time.