Comparisment

   - compare programming languages by example

27

Writing files

Write data to files.

Created by sam

Add a new answer

Either add an answer for a new language or add an alternative solution.
A description is only necessary when the code is not suitably 'obvious' or to justify an alternative approach.

Haskell

writeToFileName :: IO ()
writeToFileName = writeFile "fileName" "contents"

From the Prelude, write a string to a file.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

import Prelude hiding (writeFile)
import Data.ByteString (writeFile)
import Data.ByteString.Char8 (pack)

writeToFileName :: IO ()
writeToFileName = writeFile "fileName" (pack "contents")

Write a 'ByteString' with 8bit characters to a file.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

import Prelude hiding (writeFile)
import Data.ByteString.Lazy (writeFile)
import Data.ByteString.Lazy.Char8 (pack)

writeToFileName :: IO ()
writeToFileName = writeFile "fileName" (pack "contents")

Write a lazy 'ByteString' with 8bit characters to a file.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

import Prelude hiding (writeFile)
import Data.Text (pack)
import Data.Text.IO (writeFile)

writeToFileName :: IO ()
writeToFileName = writeFile "fileName" (pack "contents")

Write strict 'Text' to a file.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Python

def writeToFileName():
  return (open('fileName','w').write('Contents').close())

Open a file object in 'w'rite mode, write contents and close.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.