tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
5
Task/Read-entire-file/0DESCRIPTION
Normal file
5
Task/Read-entire-file/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Load the entire contents of some text file as a single string variable.
|
||||
|
||||
If applicable, discuss: encoding selection, the possibility of memory-mapping.
|
||||
|
||||
Of course, one should avoid reading an entire file at once if the file is large and the task can be accomplished incrementally instead (in which case check [[File IO]]); this is for those cases where having the entire file is actually what is wanted.
|
||||
2
Task/Read-entire-file/1META.yaml
Normal file
2
Task/Read-entire-file/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: File handling
|
||||
7
Task/Read-entire-file/ALGOL-68/read-entire-file-1.alg
Normal file
7
Task/Read-entire-file/ALGOL-68/read-entire-file-1.alg
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
MODE BOOK = FLEX[0]FLEX[0]FLEX[0]CHAR; ¢ pages of lines of characters ¢
|
||||
BOOK book;
|
||||
|
||||
FILE book file;
|
||||
INT errno = open(book file, "book.txt", stand in channel);
|
||||
|
||||
get(book file, book)
|
||||
2
Task/Read-entire-file/ALGOL-68/read-entire-file-2.alg
Normal file
2
Task/Read-entire-file/ALGOL-68/read-entire-file-2.alg
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
FILE cached book file;
|
||||
associate(cached book file, book)
|
||||
13
Task/Read-entire-file/AWK/read-entire-file.awk
Normal file
13
Task/Read-entire-file/AWK/read-entire-file.awk
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#!/usr/bin/awk -f
|
||||
BEGIN {
|
||||
## empty record separate,
|
||||
RS="";
|
||||
## read line (i.e. whole file) into $0
|
||||
getline;
|
||||
## print line number and content of line
|
||||
print "=== line "NR,":",$0;
|
||||
}
|
||||
{
|
||||
## no further line is read printed
|
||||
print "=== line "NR,":",$0;
|
||||
}
|
||||
23
Task/Read-entire-file/Ada/read-entire-file-1.ada
Normal file
23
Task/Read-entire-file/Ada/read-entire-file-1.ada
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
with Ada.Directories,
|
||||
Ada.Direct_IO,
|
||||
Ada.Text_IO;
|
||||
|
||||
procedure Whole_File is
|
||||
|
||||
File_Name : String := "whole_file.adb";
|
||||
File_Size : Natural := Natural (Ada.Directories.Size (File_Name));
|
||||
|
||||
subtype File_String is String (1 .. File_Size);
|
||||
package File_String_IO is new Ada.Direct_IO (File_String);
|
||||
|
||||
File : File_String_IO.File_Type;
|
||||
Contents : File_String;
|
||||
|
||||
begin
|
||||
File_String_IO.Open (File, Mode => File_String_IO.In_File,
|
||||
Name => File_Name);
|
||||
File_String_IO.Read (File, Item => Contents);
|
||||
File_String_IO.Close (File);
|
||||
|
||||
Ada.Text_IO.Put (Contents);
|
||||
end Whole_File;
|
||||
35
Task/Read-entire-file/Ada/read-entire-file-2.ada
Normal file
35
Task/Read-entire-file/Ada/read-entire-file-2.ada
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
with Ada.Text_IO,
|
||||
POSIX.IO,
|
||||
POSIX.Memory_Mapping,
|
||||
System.Storage_Elements;
|
||||
|
||||
procedure Read_Entire_File is
|
||||
|
||||
use POSIX, POSIX.IO, POSIX.Memory_Mapping;
|
||||
use System.Storage_Elements;
|
||||
|
||||
Text_File : File_Descriptor;
|
||||
Text_Size : System.Storage_Elements.Storage_Offset;
|
||||
Text_Address : System.Address;
|
||||
|
||||
begin
|
||||
Text_File := Open (Name => "read_entire_file.adb",
|
||||
Mode => Read_Only);
|
||||
Text_Size := Storage_Offset (File_Size (Text_File));
|
||||
Text_Address := Map_Memory (Length => Text_Size,
|
||||
Protection => Allow_Read,
|
||||
Mapping => Map_Shared,
|
||||
File => Text_File,
|
||||
Offset => 0);
|
||||
|
||||
declare
|
||||
Text : String (1 .. Natural (Text_Size));
|
||||
for Text'Address use Text_Address;
|
||||
begin
|
||||
Ada.Text_IO.Put (Text);
|
||||
end;
|
||||
|
||||
Unmap_Memory (First => Text_Address,
|
||||
Length => Text_Size);
|
||||
Close (File => Text_File);
|
||||
end Read_Entire_File;
|
||||
1
Task/Read-entire-file/AutoHotkey/read-entire-file.ahk
Normal file
1
Task/Read-entire-file/AutoHotkey/read-entire-file.ahk
Normal file
|
|
@ -0,0 +1 @@
|
|||
fileread, varname, C:\filename.txt ; adding "MsgBox %varname%" (no quotes) to the next line will display the file contents.
|
||||
5
Task/Read-entire-file/BASIC/read-entire-file.basic
Normal file
5
Task/Read-entire-file/BASIC/read-entire-file.basic
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
DIM f AS STRING
|
||||
OPEN "file.txt" FOR BINARY AS 1
|
||||
f = SPACE$(LOF(1))
|
||||
GET #1, 1, f
|
||||
CLOSE 1
|
||||
6
Task/Read-entire-file/BBC-BASIC/read-entire-file-1.bbc
Normal file
6
Task/Read-entire-file/BBC-BASIC/read-entire-file-1.bbc
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
file% = OPENIN("input.txt")
|
||||
strvar$ = ""
|
||||
WHILE NOT EOF#file%
|
||||
strvar$ += CHR$(BGET#file%)
|
||||
ENDWHILE
|
||||
CLOSE #file%
|
||||
4
Task/Read-entire-file/BBC-BASIC/read-entire-file-2.bbc
Normal file
4
Task/Read-entire-file/BBC-BASIC/read-entire-file-2.bbc
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
file% = OPENIN("input.txt")
|
||||
strvar$ = STRING$(EXT#file%, " ")
|
||||
SYS "ReadFile", @hfile%(file%), !^strvar$, EXT#file%, ^temp%, 0
|
||||
CLOSE #file%
|
||||
4
Task/Read-entire-file/Brainf---/read-entire-file.bf
Normal file
4
Task/Read-entire-file/Brainf---/read-entire-file.bf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
> Keep cell 0 at 0 as a sentinel value
|
||||
,[>,] Read into successive cells until EOF
|
||||
<[<] Go all the way back to the beginning
|
||||
>[.>] Print successive cells while nonzero
|
||||
3
Task/Read-entire-file/Brat/read-entire-file.brat
Normal file
3
Task/Read-entire-file/Brat/read-entire-file.brat
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
include :file
|
||||
|
||||
file.read file_name
|
||||
18
Task/Read-entire-file/C++/read-entire-file.cpp
Normal file
18
Task/Read-entire-file/C++/read-entire-file.cpp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <iterator>
|
||||
|
||||
int main( ) {
|
||||
std::ifstream infile( "sample.txt" ) ;
|
||||
if ( infile ) {
|
||||
std::string fileData( ( std::istreambuf_iterator<char> ( infile ) ) ,
|
||||
std::istreambuf_iterator<char> ( ) ) ;
|
||||
infile.close( ) ; ;
|
||||
return 0 ;
|
||||
}
|
||||
else {
|
||||
std::cout << "file not found!\n" ;
|
||||
return 1 ;
|
||||
}
|
||||
}
|
||||
9
Task/Read-entire-file/C-sharp/read-entire-file.cs
Normal file
9
Task/Read-entire-file/C-sharp/read-entire-file.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using System.IO;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var fileContents = File.ReadAllText("c:\\autoexec.bat");
|
||||
}
|
||||
}
|
||||
28
Task/Read-entire-file/C/read-entire-file-1.c
Normal file
28
Task/Read-entire-file/C/read-entire-file-1.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char *buffer;
|
||||
FILE *fh = fopen("readentirefile.c", "rb");
|
||||
if ( fh != NULL )
|
||||
{
|
||||
fseek(fh, 0L, SEEK_END);
|
||||
long s = ftell(fh);
|
||||
rewind(fh);
|
||||
buffer = malloc(s);
|
||||
if ( buffer != NULL )
|
||||
{
|
||||
fread(buffer, s, 1, fh);
|
||||
// we can now close the file
|
||||
fclose(fh); fh = NULL;
|
||||
|
||||
// do something, e.g.
|
||||
fwrite(buffer, s, 1, stdout);
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
if (fh != NULL) fclose(fh);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
29
Task/Read-entire-file/C/read-entire-file-2.c
Normal file
29
Task/Read-entire-file/C/read-entire-file-2.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char *buffer;
|
||||
struct stat s;
|
||||
|
||||
int fd = open("readentirefile_mm.c", O_RDONLY);
|
||||
if (fd < 0 ) return EXIT_FAILURE;
|
||||
fstat(fd, &s);
|
||||
/* PROT_READ disallows writing to buffer: will segv */
|
||||
buffer = mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
|
||||
if ( buffer != (void*)-1 )
|
||||
{
|
||||
/* do something */
|
||||
fwrite(buffer, s.st_size, 1, stdout);
|
||||
munmap(buffer, s.st_size);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
1
Task/Read-entire-file/CMake/read-entire-file-1.cmake
Normal file
1
Task/Read-entire-file/CMake/read-entire-file-1.cmake
Normal file
|
|
@ -0,0 +1 @@
|
|||
file(READ /etc/passwd string)
|
||||
1
Task/Read-entire-file/CMake/read-entire-file-2.cmake
Normal file
1
Task/Read-entire-file/CMake/read-entire-file-2.cmake
Normal file
|
|
@ -0,0 +1 @@
|
|||
file(READ /etc/pwd.db string HEX)
|
||||
2
Task/Read-entire-file/Clojure/read-entire-file.clj
Normal file
2
Task/Read-entire-file/Clojure/read-entire-file.clj
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(slurp "myfile.txt")
|
||||
(slurp "my-utf8-file.txt" "UTF-8")
|
||||
5
Task/Read-entire-file/Common-Lisp/read-entire-file.lisp
Normal file
5
Task/Read-entire-file/Common-Lisp/read-entire-file.lisp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(defun file-string (path)
|
||||
(with-open-file (stream path)
|
||||
(let ((data (make-string (file-length stream))))
|
||||
(read-sequence data stream)
|
||||
data)))
|
||||
9
Task/Read-entire-file/D/read-entire-file.d
Normal file
9
Task/Read-entire-file/D/read-entire-file.d
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import std.file: read, readText;
|
||||
|
||||
void main() {
|
||||
// To read a whole file into a dynamic array of unsigned bytes:
|
||||
auto data = cast(ubyte[])read("unixdict.txt");
|
||||
|
||||
// To read a whole file into a validated UTF-8 string:
|
||||
string txt = readText("unixdict.txt");
|
||||
}
|
||||
22
Task/Read-entire-file/Delphi/read-entire-file-1.delphi
Normal file
22
Task/Read-entire-file/Delphi/read-entire-file-1.delphi
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
program ReadAll;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses Classes;
|
||||
|
||||
var
|
||||
i: Integer;
|
||||
lList: TStringList;
|
||||
begin
|
||||
lList := TStringList.Create;
|
||||
try
|
||||
lList.LoadFromFile('c:\input.txt');
|
||||
// Write everything at once
|
||||
Writeln(lList.Text);
|
||||
// Write one line at a time
|
||||
for i := 0 to lList.Count - 1 do
|
||||
Writeln(lList[i]);
|
||||
finally
|
||||
lList.Free;
|
||||
end;
|
||||
end.
|
||||
14
Task/Read-entire-file/Delphi/read-entire-file-2.delphi
Normal file
14
Task/Read-entire-file/Delphi/read-entire-file-2.delphi
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
program ReadAll;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils, IOUtils;
|
||||
|
||||
begin
|
||||
// with default encoding:
|
||||
Writeln(TFile.ReadAllText('C:\autoexec.bat'));
|
||||
// with encoding specified:
|
||||
Writeln(TFile.ReadAllText('C:\autoexec.bat', TEncoding.ASCII));
|
||||
Readln;
|
||||
end.
|
||||
1
Task/Read-entire-file/E/read-entire-file.e
Normal file
1
Task/Read-entire-file/E/read-entire-file.e
Normal file
|
|
@ -0,0 +1 @@
|
|||
<file:foo.txt>.getText()
|
||||
1
Task/Read-entire-file/Erlang/read-entire-file.erl
Normal file
1
Task/Read-entire-file/Erlang/read-entire-file.erl
Normal file
|
|
@ -0,0 +1 @@
|
|||
{ok, B} = file:read_file("myfile.txt").
|
||||
16
Task/Read-entire-file/Euphoria/read-entire-file.euphoria
Normal file
16
Task/Read-entire-file/Euphoria/read-entire-file.euphoria
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
function load_file(sequence filename)
|
||||
integer fn,c
|
||||
sequence data
|
||||
fn = open(filename,"r") -- "r" for text files, "rb" for binary files
|
||||
if (fn = -1) then return {} end if -- failed to open the file
|
||||
|
||||
data = {} -- init to empty sequence
|
||||
c = getc(fn) -- prime the char buffer
|
||||
while (c != -1) do -- while not EOF
|
||||
data &= c -- append each character
|
||||
c = getc(fn) -- next char
|
||||
end while
|
||||
|
||||
close(fn)
|
||||
return data
|
||||
end function
|
||||
7
Task/Read-entire-file/Factor/read-entire-file.factor
Normal file
7
Task/Read-entire-file/Factor/read-entire-file.factor
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
USING: io.encodings.ascii io.encodings.binary io.files ;
|
||||
|
||||
! to read entire file as binary
|
||||
"foo.txt" binary file-contents
|
||||
|
||||
! to read entire file as lines of text
|
||||
"foo.txt" ascii file-lines
|
||||
8
Task/Read-entire-file/Fantom/read-entire-file.fantom
Normal file
8
Task/Read-entire-file/Fantom/read-entire-file.fantom
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class ReadString
|
||||
{
|
||||
public static Void main (Str[] args)
|
||||
{
|
||||
Str contents := File(args[0].toUri).readAllStr
|
||||
echo ("contents: $contents")
|
||||
}
|
||||
}
|
||||
1
Task/Read-entire-file/Forth/read-entire-file.fth
Normal file
1
Task/Read-entire-file/Forth/read-entire-file.fth
Normal file
|
|
@ -0,0 +1 @@
|
|||
s" foo.txt" slurp-file ( str len )
|
||||
2
Task/Read-entire-file/Frink/read-entire-file.frink
Normal file
2
Task/Read-entire-file/Frink/read-entire-file.frink
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a = read["file:yourfile.txt"]
|
||||
b = read["file:yourfile.txt", "UTF-8"]
|
||||
3
Task/Read-entire-file/GAP/read-entire-file.gap
Normal file
3
Task/Read-entire-file/GAP/read-entire-file.gap
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
InputTextFile("input.txt");
|
||||
s := ReadAll(f);; # two semicolons to hide the result, which may be long
|
||||
CloseStream(f);
|
||||
1
Task/Read-entire-file/GUISS/read-entire-file.guiss
Normal file
1
Task/Read-entire-file/GUISS/read-entire-file.guiss
Normal file
|
|
@ -0,0 +1 @@
|
|||
Start,Programs,Accessories,Notepad,Menu:File,Open,Doubleclick:Icon:Notes.TXT,Button:OK
|
||||
4
Task/Read-entire-file/Go/read-entire-file-1.go
Normal file
4
Task/Read-entire-file/Go/read-entire-file-1.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import "io/ioutil"
|
||||
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
sv := string(data)
|
||||
25
Task/Read-entire-file/Go/read-entire-file-2.go
Normal file
25
Task/Read-entire-file/Go/read-entire-file-2.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, err := os.Open("file")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
data, err := syscall.Mmap(int(f.Fd()), 0, int(fi.Size()),
|
||||
syscall.PROT_READ, syscall.MAP_PRIVATE)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
1
Task/Read-entire-file/Groovy/read-entire-file.groovy
Normal file
1
Task/Read-entire-file/Groovy/read-entire-file.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
def fileContent = new File("c:\\file.txt").text
|
||||
2
Task/Read-entire-file/Haskell/read-entire-file-1.hs
Normal file
2
Task/Read-entire-file/Haskell/read-entire-file-1.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
do text <- readFile filepath
|
||||
-- do stuff with text
|
||||
4
Task/Read-entire-file/Haskell/read-entire-file-2.hs
Normal file
4
Task/Read-entire-file/Haskell/read-entire-file-2.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
eagerReadFile :: FilePath -> IO String
|
||||
eagerReadFile filepath = do
|
||||
text <- readFile filepath
|
||||
last text `seq` return text
|
||||
1
Task/Read-entire-file/Icon/read-entire-file-1.icon
Normal file
1
Task/Read-entire-file/Icon/read-entire-file-1.icon
Normal file
|
|
@ -0,0 +1 @@
|
|||
every (fs := "") ||:= |reads(1000000)
|
||||
3
Task/Read-entire-file/Icon/read-entire-file-2.icon
Normal file
3
Task/Read-entire-file/Icon/read-entire-file-2.icon
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
every put(fL := [],|FUNC(read()))
|
||||
every (fs := "") ||:= !fL || "\n"
|
||||
fL := &null
|
||||
7
Task/Read-entire-file/Inform-7/read-entire-file.inf
Normal file
7
Task/Read-entire-file/Inform-7/read-entire-file.inf
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Home is a room.
|
||||
|
||||
The File of Testing is called "test".
|
||||
|
||||
When play begins:
|
||||
say "[text of the File of Testing]";
|
||||
end the story.
|
||||
1
Task/Read-entire-file/J/read-entire-file-1.j
Normal file
1
Task/Read-entire-file/J/read-entire-file-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
var=: 1!:1<'foo.txt'
|
||||
2
Task/Read-entire-file/J/read-entire-file-2.j
Normal file
2
Task/Read-entire-file/J/read-entire-file-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
require'jmf'
|
||||
JCHAR map_jmf_ 'var';'foo.txt'
|
||||
21
Task/Read-entire-file/Java/read-entire-file-1.java
Normal file
21
Task/Read-entire-file/Java/read-entire-file-1.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ReadFile {
|
||||
public static void main(String[] args) throws IOException{
|
||||
String fileContents = readEntireFile("./foo.txt");
|
||||
}
|
||||
|
||||
private static String readEntireFile(String filename) throws IOException {
|
||||
FileReader in = new FileReader(filename);
|
||||
StringBuilder contents = new StringBuilder();
|
||||
char[] buffer = new char[4096];
|
||||
int read = 0;
|
||||
do {
|
||||
contents.append(buffer, 0, read);
|
||||
read = in.read(buffer);
|
||||
} while (read >= 0);
|
||||
return contents.toString();
|
||||
}
|
||||
}
|
||||
20
Task/Read-entire-file/Java/read-entire-file-2.java
Normal file
20
Task/Read-entire-file/Java/read-entire-file-2.java
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import java.nio.channels.FileChannel.MapMode;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
|
||||
public class MMapReadFile {
|
||||
public static void main(String[] args) throws IOException {
|
||||
MappedByteBuffer buff = getBufferFor(new File(args[0]));
|
||||
String results = new String(buff.asCharBuffer());
|
||||
}
|
||||
|
||||
public static MappedByteBuffer getBufferFor(File f) throws IOException {
|
||||
RandomAccessFile file = new RandomAccessFile(f, "r");
|
||||
|
||||
MappedByteBuffer buffer = file.getChannel().map(MapMode.READ_ONLY, 0, f.length());
|
||||
file.close();
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
1
Task/Read-entire-file/Java/read-entire-file-3.java
Normal file
1
Task/Read-entire-file/Java/read-entire-file-3.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
String content = new Scanner(new File("foo"), "UTF-8").useDelimiter("\\A").next();
|
||||
15
Task/Read-entire-file/Java/read-entire-file-4.java
Normal file
15
Task/Read-entire-file/Java/read-entire-file-4.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import java.util.List;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.*;
|
||||
|
||||
public class ReadAll {
|
||||
public static List<String> readAllLines(String filesname){
|
||||
Path file = Paths.get(filename);
|
||||
return Files.readAllLines(file, Charset.defaultCharset());
|
||||
}
|
||||
|
||||
public static byte[] readAllBytes(String filename){
|
||||
Path file = Paths.get(filename);
|
||||
return Files.readAllBytes(file);
|
||||
}
|
||||
}
|
||||
5
Task/Read-entire-file/JavaScript/read-entire-file-1.js
Normal file
5
Task/Read-entire-file/JavaScript/read-entire-file-1.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var fso=new ActiveXObject("Scripting.FileSystemObject");
|
||||
var f=fso.OpenTextFile("c:\\myfile.txt",1);
|
||||
var s=f.ReadAll();
|
||||
f.Close();
|
||||
try{alert(s)}catch(e){WScript.Echo(s)}
|
||||
14
Task/Read-entire-file/JavaScript/read-entire-file-2.js
Normal file
14
Task/Read-entire-file/JavaScript/read-entire-file-2.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
var file = document.getElementById("fileInput").files.item(0); //a file input element
|
||||
if (file) {
|
||||
var reader = new FileReader();
|
||||
reader.readAsText(file, "UTF-8");
|
||||
reader.onload = loadedFile;
|
||||
reader.onerror = errorHandler;
|
||||
}
|
||||
function loadedFile(event) {
|
||||
var fileString = event.target.result;
|
||||
alert(fileString);
|
||||
}
|
||||
function errorHandler(event) {
|
||||
alert(event);
|
||||
}
|
||||
4
Task/Read-entire-file/Julia/read-entire-file.julia
Normal file
4
Task/Read-entire-file/Julia/read-entire-file.julia
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
sample_filepath = "/devel/myfile.txt"
|
||||
f = open(sample_filepath)
|
||||
file_contents = readall(f) # load all into a single String
|
||||
close(f)
|
||||
1
Task/Read-entire-file/Lang5/read-entire-file.lang5
Normal file
1
Task/Read-entire-file/Lang5/read-entire-file.lang5
Normal file
|
|
@ -0,0 +1 @@
|
|||
'foo.txt slurp
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
filedialog "Open a Text File","*.txt",file$
|
||||
if file$<>"" then
|
||||
open file$ for input as #1
|
||||
entire$ = input$(#1, lof(#1))
|
||||
close #1
|
||||
print entire$
|
||||
end if
|
||||
21
Task/Read-entire-file/Lua/read-entire-file.lua
Normal file
21
Task/Read-entire-file/Lua/read-entire-file.lua
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
--If the file opens with no problems, io.open will return a
|
||||
--handle to the file with methods attached.
|
||||
--If the file does not exist, io.open will return nil and
|
||||
--an error message.
|
||||
--assert will return the handle to the file if present, or
|
||||
--it will throw an error with the message returned second
|
||||
--by io.open.
|
||||
local file = assert(io.open(filename))
|
||||
--Without wrapping io.open in an assert, local file would be nil,
|
||||
--which would cause an 'attempt to index a nil value' error when
|
||||
--calling file:read.
|
||||
|
||||
--file:read takes the number of bytes to read, or a string for
|
||||
--special cases, such as "*a" to read the entire file.
|
||||
local contents = file:read'*a'
|
||||
|
||||
--If the file handle was local to the expression
|
||||
--(ie. "assert(io.open(filename)):read'a'"),
|
||||
--the file would remain open until its handle was
|
||||
--garbage collected.
|
||||
file:close()
|
||||
3
Task/Read-entire-file/MATLAB/read-entire-file.m
Normal file
3
Task/Read-entire-file/MATLAB/read-entire-file.m
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fid = fopen('filename','r');
|
||||
[str,count] = fread(fid, [1,inf], 'uint8=>char'); % s will be a character array, count has the number of bytes
|
||||
fclose(fid);
|
||||
1
Task/Read-entire-file/Maple/read-entire-file-1.maple
Normal file
1
Task/Read-entire-file/Maple/read-entire-file-1.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
s1 := readbytes( "file1.txt", infinity, TEXT ):
|
||||
1
Task/Read-entire-file/Maple/read-entire-file-2.maple
Normal file
1
Task/Read-entire-file/Maple/read-entire-file-2.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
s2 := FileTools:-Text:-ReadFile( "file2.txt" ):
|
||||
1
Task/Read-entire-file/Mathematica/read-entire-file.math
Normal file
1
Task/Read-entire-file/Mathematica/read-entire-file.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
Import["filename","String"]
|
||||
43
Task/Read-entire-file/NetRexx/read-entire-file.netrexx
Normal file
43
Task/Read-entire-file/NetRexx/read-entire-file.netrexx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
parse arg inFileName .
|
||||
|
||||
if inFileName = '' | inFileName = '.' then inFileName = './data/dwarfs.json'
|
||||
fileContents = slurp(inFileName)
|
||||
say fileContents
|
||||
|
||||
return
|
||||
|
||||
-- Slurp a file and return contents as a Rexx string
|
||||
method slurp(inFileName) public static returns Rexx
|
||||
|
||||
slurped = Rexx null
|
||||
slurpStr = StringBuilder()
|
||||
ioBuffer = byte[1024]
|
||||
inBytes = int 0
|
||||
|
||||
do
|
||||
inFile = File(inFileName)
|
||||
inFileIS = BufferedInputStream(FileInputStream(inFile))
|
||||
|
||||
loop label ioLoop until inBytes = -1
|
||||
slurpStr.append(String(ioBuffer, 0, inBytes))
|
||||
inBytes = inFileIS.read(ioBuffer)
|
||||
end ioLoop
|
||||
|
||||
catch exFNF = FileNotFoundException
|
||||
exFNF.printStackTrace
|
||||
catch exIO = IOException
|
||||
exIO.printStackTrace
|
||||
finally
|
||||
do
|
||||
inFileIS.close()
|
||||
catch ex = IOException
|
||||
ex.printStackTrace
|
||||
end
|
||||
end
|
||||
|
||||
slurped = Rexx(slurpStr.toString)
|
||||
|
||||
return slurped
|
||||
1
Task/Read-entire-file/NewLISP/read-entire-file.newlisp
Normal file
1
Task/Read-entire-file/NewLISP/read-entire-file.newlisp
Normal file
|
|
@ -0,0 +1 @@
|
|||
(read-file "filename")
|
||||
1
Task/Read-entire-file/Nimrod/read-entire-file.nimrod
Normal file
1
Task/Read-entire-file/Nimrod/read-entire-file.nimrod
Normal file
|
|
@ -0,0 +1 @@
|
|||
readFile(filename)
|
||||
7
Task/Read-entire-file/OCaml/read-entire-file-1.ocaml
Normal file
7
Task/Read-entire-file/OCaml/read-entire-file-1.ocaml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
let load_file f =
|
||||
let ic = open_in f in
|
||||
let n = in_channel_length ic in
|
||||
let s = String.create n in
|
||||
really_input ic s 0 n;
|
||||
close_in ic;
|
||||
(s)
|
||||
2
Task/Read-entire-file/OCaml/read-entire-file-2.ocaml
Normal file
2
Task/Read-entire-file/OCaml/read-entire-file-2.ocaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
type big_string =
|
||||
(char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
|
||||
9
Task/Read-entire-file/OCaml/read-entire-file-3.ocaml
Normal file
9
Task/Read-entire-file/OCaml/read-entire-file-3.ocaml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
let load_big_file filename =
|
||||
let fd = Unix.openfile filename [Unix.O_RDONLY] 0o640 in
|
||||
let len = Unix.lseek fd 0 Unix.SEEK_END in
|
||||
let _ = Unix.lseek fd 0 Unix.SEEK_SET in
|
||||
let shared = false in (* modifications are done in memory only *)
|
||||
let bstr = Bigarray.Array1.map_file fd
|
||||
Bigarray.char Bigarray.c_layout shared len in
|
||||
Unix.close fd;
|
||||
(bstr)
|
||||
7
Task/Read-entire-file/OCaml/read-entire-file-4.ocaml
Normal file
7
Task/Read-entire-file/OCaml/read-entire-file-4.ocaml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
let () =
|
||||
let bstr = load_big_file Sys.argv.(1) in
|
||||
let len = Bigarray.Array1.dim bstr in
|
||||
for i = 0 to pred len do
|
||||
let c = bstr.{i} in
|
||||
print_char c
|
||||
done
|
||||
1
Task/Read-entire-file/Objeck/read-entire-file.objeck
Normal file
1
Task/Read-entire-file/Objeck/read-entire-file.objeck
Normal file
|
|
@ -0,0 +1 @@
|
|||
string := FileReader->ReadFile("in.txt");
|
||||
32
Task/Read-entire-file/Objective-C/read-entire-file.m
Normal file
32
Task/Read-entire-file/Objective-C/read-entire-file.m
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/*** 0. PREPARATION */
|
||||
// We need a text file to read; let's redirect a C string to a new file
|
||||
// using the shell by way of the stdlib system() function.
|
||||
system ("echo \"Hello, World!\" > ~/HelloRosetta");
|
||||
|
||||
|
||||
|
||||
/*** 1. THE TASK */
|
||||
// Instantiate an NSString which describes the filesystem location of
|
||||
// the file we will be reading.
|
||||
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"HelloRosetta"];
|
||||
|
||||
// The selector we're going to use to complete this task,
|
||||
// stringWithContentsOfFile:encoding:error, has an optional `error'
|
||||
// parameter which can be used to return information about any
|
||||
// errors it might run into. It's optional, but we'll create an NSError
|
||||
// anyways to demonstrate best practice.
|
||||
NSError *anError;
|
||||
|
||||
// And finally, the task: read and store the contents of a file as an
|
||||
// NSString.
|
||||
NSString *aString = [NSString stringWithContentsOfFile:filePath
|
||||
encoding:NSUTF8StringEncoding
|
||||
error:&anError];
|
||||
|
||||
// If the file read was unsuccessful, display the error description.
|
||||
// Otherwise, display the NSString.
|
||||
if (!aString) {
|
||||
NSLog(@"%@", [anError localizedDescription]);
|
||||
} else {
|
||||
NSLog(@"%@", aString);
|
||||
}
|
||||
6
Task/Read-entire-file/Oz/read-entire-file.oz
Normal file
6
Task/Read-entire-file/Oz/read-entire-file.oz
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
declare
|
||||
FileHandle = {New Open.file init(name:"test.txt")}
|
||||
FileContents = {FileHandle read(size:all list:$)}
|
||||
in
|
||||
{FileHandle close}
|
||||
{System.printInfo FileContents}
|
||||
1
Task/Read-entire-file/PARI-GP/read-entire-file.pari
Normal file
1
Task/Read-entire-file/PARI-GP/read-entire-file.pari
Normal file
|
|
@ -0,0 +1 @@
|
|||
text=read("file.txt");
|
||||
1
Task/Read-entire-file/PHP/read-entire-file.php
Normal file
1
Task/Read-entire-file/PHP/read-entire-file.php
Normal file
|
|
@ -0,0 +1 @@
|
|||
file_get_contents($filename)
|
||||
1
Task/Read-entire-file/PL-I/read-entire-file.pli
Normal file
1
Task/Read-entire-file/PL-I/read-entire-file.pli
Normal file
|
|
@ -0,0 +1 @@
|
|||
get file (in) edit ((substr(s, i, 1) do i = 1 to 32767)) (a);
|
||||
1
Task/Read-entire-file/Perl-6/read-entire-file.pl6
Normal file
1
Task/Read-entire-file/Perl-6/read-entire-file.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
my $string = slurp 'sample.txt';
|
||||
3
Task/Read-entire-file/Perl/read-entire-file-1.pl
Normal file
3
Task/Read-entire-file/Perl/read-entire-file-1.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
open my $fh, $filename;
|
||||
my $text = do { local( $/ ); <$fh> };
|
||||
close $fh;
|
||||
3
Task/Read-entire-file/Perl/read-entire-file-2.pl
Normal file
3
Task/Read-entire-file/Perl/read-entire-file-2.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
open my $fh, $filename;
|
||||
my $text;read $fh, $text, -s $filename;
|
||||
close $fh;
|
||||
2
Task/Read-entire-file/Perl/read-entire-file-3.pl
Normal file
2
Task/Read-entire-file/Perl/read-entire-file-3.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
use File::Slurp;
|
||||
my $text = read_file($filename);
|
||||
6
Task/Read-entire-file/Perl/read-entire-file-4.pl
Normal file
6
Task/Read-entire-file/Perl/read-entire-file-4.pl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
use IO::All;
|
||||
$text = io($filename)->all;
|
||||
$text = io($filename)->utf8->all;
|
||||
@text = io($filename)->slurp;
|
||||
$text < io($filename);
|
||||
io($filename) > $text;
|
||||
1
Task/Read-entire-file/Perl/read-entire-file-5.pl
Normal file
1
Task/Read-entire-file/Perl/read-entire-file-5.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
perl -n -0777 -e 'print "file len: ".length' stuff.txt
|
||||
1
Task/Read-entire-file/PicoLisp/read-entire-file-1.l
Normal file
1
Task/Read-entire-file/PicoLisp/read-entire-file-1.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
(in "file" (till NIL T))
|
||||
1
Task/Read-entire-file/PicoLisp/read-entire-file-2.l
Normal file
1
Task/Read-entire-file/PicoLisp/read-entire-file-2.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
(in "file" (till NIL))
|
||||
1
Task/Read-entire-file/PicoLisp/read-entire-file-3.l
Normal file
1
Task/Read-entire-file/PicoLisp/read-entire-file-3.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
(in "file" (make (while (char) (link @))))
|
||||
1
Task/Read-entire-file/Pike/read-entire-file.pike
Normal file
1
Task/Read-entire-file/Pike/read-entire-file.pike
Normal file
|
|
@ -0,0 +1 @@
|
|||
string content=Stdio.File("foo.txt")->read();
|
||||
1
Task/Read-entire-file/PowerShell/read-entire-file-1.psh
Normal file
1
Task/Read-entire-file/PowerShell/read-entire-file-1.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
Get-Content foo.txt
|
||||
1
Task/Read-entire-file/PowerShell/read-entire-file-2.psh
Normal file
1
Task/Read-entire-file/PowerShell/read-entire-file-2.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
Get-Content foo.txt -Encoding UTF8
|
||||
1
Task/Read-entire-file/PowerShell/read-entire-file-3.psh
Normal file
1
Task/Read-entire-file/PowerShell/read-entire-file-3.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
(Get-Content foo.txt) -join "`n"
|
||||
10
Task/Read-entire-file/PureBasic/read-entire-file-1.purebasic
Normal file
10
Task/Read-entire-file/PureBasic/read-entire-file-1.purebasic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
Number.b = ReadByte(#File)
|
||||
Length.i = ReadData(#File, *MemoryBuffer, LengthToRead)
|
||||
Number.c = ReadCharacter(#File)
|
||||
Number.d = ReadDouble(#File)
|
||||
Number.f = ReadFloat(#File)
|
||||
Number.i = ReadInteger(#File)
|
||||
Number.l = ReadLong(#File)
|
||||
Number.q = ReadQuad(#File)
|
||||
Text$ = ReadString(#File [, Flags])
|
||||
Number.w = ReadWord(#File)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
If ReadFile(0, "RC.txt")
|
||||
Variable$=ReadString(0)
|
||||
CloseFile(0)
|
||||
EndIf
|
||||
14
Task/Read-entire-file/PureBasic/read-entire-file-3.purebasic
Normal file
14
Task/Read-entire-file/PureBasic/read-entire-file-3.purebasic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
Title$="Select a file"
|
||||
Pattern$="Text (.txt)|*.txt|All files (*.*)|*.*"
|
||||
fileName$ = OpenFileRequester(Title$,"",Pattern$,0)
|
||||
If fileName$
|
||||
If ReadFile(0, fileName$)
|
||||
length = Lof(0)
|
||||
*MemoryID = AllocateMemory(length)
|
||||
If *MemoryID
|
||||
bytes = ReadData(0, *MemoryID, length)
|
||||
MessageRequester("Info",Str(bytes)+" was read")
|
||||
EndIf
|
||||
CloseFile(0)
|
||||
EndIf
|
||||
EndIf
|
||||
1
Task/Read-entire-file/Python/read-entire-file-1.py
Normal file
1
Task/Read-entire-file/Python/read-entire-file-1.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
open(filename).read()
|
||||
1
Task/Read-entire-file/Python/read-entire-file-2.py
Normal file
1
Task/Read-entire-file/Python/read-entire-file-2.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
open(filename, encoding='utf-8').read()
|
||||
5
Task/Read-entire-file/R/read-entire-file.r
Normal file
5
Task/Read-entire-file/R/read-entire-file.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
fname <- "notes.txt"
|
||||
len <- file.info(fname)$size
|
||||
conn <- file(fname, 'r')
|
||||
contents <- readChar(conn, len)
|
||||
close(conn)
|
||||
11
Task/Read-entire-file/REALbasic/read-entire-file.realbasic
Normal file
11
Task/Read-entire-file/REALbasic/read-entire-file.realbasic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
Function readFile(theFile As FolderItem, txtEncode As TextEncoding = Nil) As String
|
||||
Dim fileContents As String
|
||||
Dim tis As TextInputStream
|
||||
tis = tis.Open(theFile)
|
||||
fileContents = tis.ReadAll(txtEncode)
|
||||
tis.Close
|
||||
Return fileContents
|
||||
|
||||
Exception err As NilObjectException
|
||||
MsgBox("File Not Found.")
|
||||
End Function
|
||||
2
Task/Read-entire-file/REBOL/read-entire-file.rebol
Normal file
2
Task/Read-entire-file/REBOL/read-entire-file.rebol
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
read %my-file ; read as text
|
||||
read/binary %my-file ; preserve contents exactly
|
||||
9
Task/Read-entire-file/REXX/read-entire-file.rexx
Normal file
9
Task/Read-entire-file/REXX/read-entire-file.rexx
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/*REXX program to read a file and store it as a continuous char string. */
|
||||
|
||||
iFID = 'a_file' /*name of the input file. */
|
||||
aString =
|
||||
|
||||
do while lines(iFID)\==0 /*read until finished. */
|
||||
aString = aString || linein(iFID) /*append input to Astring. */
|
||||
end /*while*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
1
Task/Read-entire-file/Racket/read-entire-file.rkt
Normal file
1
Task/Read-entire-file/Racket/read-entire-file.rkt
Normal file
|
|
@ -0,0 +1 @@
|
|||
(file->string "foo.txt")
|
||||
2
Task/Read-entire-file/Retro/read-entire-file.retro
Normal file
2
Task/Read-entire-file/Retro/read-entire-file.retro
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
with files'
|
||||
here "input.txt" slurp
|
||||
5
Task/Read-entire-file/Ruby/read-entire-file-1.rb
Normal file
5
Task/Read-entire-file/Ruby/read-entire-file-1.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Read entire text file.
|
||||
str = IO.read "foobar.txt"
|
||||
|
||||
# It can also read a subprocess.
|
||||
str = IO.read "| grep ftp /etc/services"
|
||||
2
Task/Read-entire-file/Ruby/read-entire-file-2.rb
Normal file
2
Task/Read-entire-file/Ruby/read-entire-file-2.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
path = "|strange-name.txt"
|
||||
str = File.open(path) {|f| f.read}
|
||||
2
Task/Read-entire-file/Ruby/read-entire-file-3.rb
Normal file
2
Task/Read-entire-file/Ruby/read-entire-file-3.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Read entire binary file.
|
||||
str = File.open(path, "rb") {|f| f.read}
|
||||
5
Task/Read-entire-file/Ruby/read-entire-file-4.rb
Normal file
5
Task/Read-entire-file/Ruby/read-entire-file-4.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Read EUC-JP text from file.
|
||||
str = File.open(path, "r:euc-jp") {|f| f.read}
|
||||
|
||||
# Read EUC-JP text from file; transcode text from EUC-JP to UTF-8.
|
||||
str = File.open(path, "r:euc-jp:utf-8") {|f| f.read}
|
||||
6
Task/Read-entire-file/SNOBOL4/read-entire-file.sno
Normal file
6
Task/Read-entire-file/SNOBOL4/read-entire-file.sno
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
input(.inbin,21,"filename.txt [-r524288]") :f(end)
|
||||
rdlp buf = inbin :s(rdlp)
|
||||
*
|
||||
* now process the 'buf' containing the file
|
||||
*
|
||||
end
|
||||
1
Task/Read-entire-file/Smalltalk/read-entire-file-1.st
Normal file
1
Task/Read-entire-file/Smalltalk/read-entire-file-1.st
Normal file
|
|
@ -0,0 +1 @@
|
|||
(StandardFileStream oldFileNamed: 'foo.txt') contents
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue