Logo

Programming-Idioms

Create the string lines from the content of the file with filename f.
Implementation
Fortran

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Fortran implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
byte[] encoded = Files.readAllBytes(Paths.get(f));
String lines = new String(encoded, StandardCharsets.UTF_8);
open my $fh, '<', $f;
my $lines = do { local $/; <$fh> };
close $fh;
use std::io::prelude::*;
use std::fs::File;
let mut file = File::open(f)?;
let mut lines = String::new();
file.read_to_string(&mut lines)?;
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
FILE *file;
size_t len=0;
char *lines;
assert(file=fopen(f,"rb"));
assert(lines=malloc(sizeof(char)));

while(!feof(file))
{
	assert(lines=realloc(lines,(len+0x1000)*sizeof(char)));
	len+=fread(lines,1,0x1000,file);
}

assert(lines=realloc(lines,len*sizeof(char)));
import std.file;
string lines = cast(string) read(f, size_t.max);
import std.file;
auto lines = f.readText;
Dim lines = IO.File.ReadAllText(f)
import "os"
b, err := os.ReadFile(f)
if err != nil {
	// Handle error...
}
lines := string(b)
lines = open(f).read()
uses Classes;
var
 _lines, _f: String;
 SL: TStringList;
begin
  SL := TStringList.Create;
  SL.LoadFromFile(_f);
  _lines := SL.Text;
  SL.Free;
end;
import "dart:io";
var lines = new File(f).readAsStringSync();
$lines = file_get_contents('f');
if ($lines === false) {
    // handle error...
}
lines = File.read(f)
do lines <- readFile f; putStr lines
lines = File.read!(f)
{ok, Lines} = file:read_file(F).
lines = io.input(f):read('a')
System.IO;
string lines = File.ReadAllText(f);
var fs = require('fs');
fs.readFile(f, (err, lines) => {
    if (err) {
        // Handle error...
    }

    // Work with `lines` here.
}
import scala.io.Source
val lines = Source.fromFile(filename).getLines().mkString("\n")
use std::fs;
let lines = fs::read_to_string(f).expect("Can't read file.");
#include <sstream>
std::string fromFile(std::string _f)
{
    std::ifstream t(_f);
    t.seekg(0, std::ios::end);
    size_t size = t.tellg();
    std::string buffer(size, ' ');
    t.seekg(0);
    t.read(&buffer[0], size); 
}
(def lines (slurp f))
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
int err = 0;
int fd = 0;
void * ptr = NULL;
struct stat st;
if ((fd = open (f, O_RDONLY))
&& (err = fstat (fd, &st)) == 0
&& (ptr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) != -1) {
    const char * lines = ptr;
    puts (lines);
    munmap (ptr, st.st_size);
    close (fd);
}
with open(f) as fo:
    lines = fo.read()
import java.io.File
File(f).readText()
@import Foundation;
NSString *lines=[NSString stringWithContentsOfFile:f encoding:e error:NULL];
(with-open-file (stream f)
  (uiop:slurp-stream-string stream))
| lines |
lines := f asFilename readStream upToEnd.
(defvar *lines*
   (with-open-file (stream f)
      (let ((contents (make-string (file-length stream))))
            (read-sequence contents stream)
      :return contents)))