2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -4,11 +4,13 @@
|
|||
{{omit from|TI-89 BASIC|No filesystem.}}
|
||||
{{omit from|Unlambda|Does not know files.}}
|
||||
|
||||
;Task:
|
||||
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
|
||||
Of course, in practice 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.
|
||||
<br><br>
|
||||
|
|
|
|||
19
Task/Read-entire-file/C/read-entire-file-3.c
Normal file
19
Task/Read-entire-file/C/read-entire-file-3.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
HANDLE hFile, hMap;
|
||||
DWORD filesize;
|
||||
char *p;
|
||||
|
||||
hFile = CreateFile("mmap_win.c", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
filesize = GetFileSize(hFile, NULL);
|
||||
hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||
p = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
|
||||
|
||||
fwrite(p, filesize, 1, stdout);
|
||||
|
||||
CloseHandle(hMap);
|
||||
CloseHandle(hFile);
|
||||
return 0;
|
||||
}
|
||||
14
Task/Read-entire-file/Fortran/read-entire-file-1.f
Normal file
14
Task/Read-entire-file/Fortran/read-entire-file-1.f
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
program read_file
|
||||
implicit none
|
||||
integer :: n
|
||||
character(:), allocatable :: s
|
||||
|
||||
open(unit=10, file="read_file.f90", action="read", &
|
||||
form="unformatted", access="stream")
|
||||
inquire(unit=10, size=n)
|
||||
allocate(character(n) :: s)
|
||||
read(10) s
|
||||
close(10)
|
||||
|
||||
print "(A)", s
|
||||
end program
|
||||
22
Task/Read-entire-file/Fortran/read-entire-file-2.f
Normal file
22
Task/Read-entire-file/Fortran/read-entire-file-2.f
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
program file_win
|
||||
use kernel32
|
||||
use iso_c_binding
|
||||
implicit none
|
||||
|
||||
integer(HANDLE) :: hFile, hMap, hOutput
|
||||
integer(DWORD) :: fileSize
|
||||
integer(LPVOID) :: ptr
|
||||
integer(LPDWORD) :: charsWritten
|
||||
integer(BOOL) :: s
|
||||
|
||||
hFile = CreateFile("file_win.f90" // c_null_char, GENERIC_READ, &
|
||||
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
|
||||
filesize = GetFileSize(hFile, NULL)
|
||||
hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL)
|
||||
ptr = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)
|
||||
|
||||
hOutput = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||
s = WriteConsole(hOutput, ptr, fileSize, transfer(c_loc(charsWritten), 0_c_intptr_t), NULL)
|
||||
s = CloseHandle(hMap)
|
||||
s = CloseHandle(hFile)
|
||||
end program
|
||||
|
|
@ -16,6 +16,7 @@ public class ReadFile {
|
|||
contents.append(buffer, 0, read);
|
||||
read = in.read(buffer);
|
||||
} while (read >= 0);
|
||||
in.close();
|
||||
return contents.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
1
Task/Read-entire-file/Perl-6/read-entire-file-2.pl6
Normal file
1
Task/Read-entire-file/Perl-6/read-entire-file-2.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
my $string = slurp 'sample.txt', :enc<UTF-16>;
|
||||
1
Task/Read-entire-file/Perl-6/read-entire-file-3.pl6
Normal file
1
Task/Read-entire-file/Perl-6/read-entire-file-3.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
my $string = 'sample.txt'.IO.slurp;
|
||||
|
|
@ -1 +1,2 @@
|
|||
my $text = do { local( @ARGV, $/ ) = ( $filename ); <> };
|
||||
use File::Slurper 'read_text';
|
||||
my $text = read_text($filename, $data);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,2 @@
|
|||
open my $fh, $filename;
|
||||
my $text; read $fh, $text, -s $filename;
|
||||
close $fh;
|
||||
use Path::Tiny;
|
||||
my $text = path($filename)->slurp_utf8;
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
use File::Slurp;
|
||||
my $text = read_file($filename);
|
||||
use IO::All;
|
||||
$text = io($filename)->utf8->all;
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
use Perl6::Slurp qw(slurp);
|
||||
my $text = slurp($filename);
|
||||
open my $fh, '<:encoding(UTF-8)', $filename or die "Could not open '$filename': $!";
|
||||
my $text;
|
||||
read $fh, $text, -s $filename;
|
||||
close $fh;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use IO::All;
|
||||
$text = io($filename)->all;
|
||||
$text = io($filename)->utf8->all;
|
||||
@text = io($filename)->slurp;
|
||||
$text < io($filename);
|
||||
io($filename) > $text;
|
||||
my $text;
|
||||
{
|
||||
local $/ = undef;
|
||||
open my $fh, '<:encoding(UTF-8)', $filename or die "Could not open '$filename': $!";
|
||||
$text = <$fh>;
|
||||
close $fh;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
perl -n -0777 -e 'print "file len: ".length' stuff.txt
|
||||
my $text = do { local( @ARGV, $/ ) = ( $filename ); <> };
|
||||
|
|
|
|||
|
|
@ -1,3 +1 @@
|
|||
use File::Map 'map_file';
|
||||
map_file(my $str, "foo.txt");
|
||||
print $str;
|
||||
perl -n -0777 -e 'print "file len: ".length' stuff.txt
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
use Sys::Mmap;
|
||||
Sys::Mmap->new(my $str, 0, 'foo.txt')
|
||||
or die "Cannot Sys::Mmap->new: $!";
|
||||
use File::Map 'map_file';
|
||||
map_file(my $str, "foo.txt");
|
||||
print $str;
|
||||
|
|
|
|||
4
Task/Read-entire-file/Perl/read-entire-file-9.pl
Normal file
4
Task/Read-entire-file/Perl/read-entire-file-9.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use Sys::Mmap;
|
||||
Sys::Mmap->new(my $str, 0, 'foo.txt')
|
||||
or die "Cannot Sys::Mmap->new: $!";
|
||||
print $str;
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
/*REXX program reads a file and stores it as a continuous character str.*/
|
||||
iFID = 'a_file' /*name of the input file. */
|
||||
aString = /*value of file's contents so far*/
|
||||
/* [↓] read file line-by-line. */
|
||||
do while lines(iFID) \== 0 /*read file's lines 'til finished*/
|
||||
aString = aString || linein(iFID) /*append a (file) line to aString*/
|
||||
end /*while*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
/*REXX program reads an entire file line-by-line and stores it as a continuous string.*/
|
||||
parse arg iFID . /*obtain optional argument from the CL.*/
|
||||
if iFID=='' then iFID= 'a_file' /*Not specified? Then use the default.*/
|
||||
$= /*a string of file's contents (so far).*/
|
||||
do while lines(iFID)\==0 /*read the file's lines until finished.*/
|
||||
$=$ || linein(iFID) /*append a (file's) line to the string,*/
|
||||
end /*while*/ /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue