Read entire file

Revision as of 23:50, 29 June 2010 by rosettacode>NevilleDNZ (→‎[[Read_entire_file#ALGOL 68]]: Still todo: add implementation specific example.)

Load the entire contents of some text file as a single string variable.

Task
Read entire file
You are encouraged to solve this task according to the task description, using any language you may know.

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.

ALGOL 68

In official ALGOL 68 a FILE is composed of pages, lines and characters, however for ALGOL 68 Genie and ELLA ALGOL 68RS this concept is not supported as they adopt the Unix concept of files being "flat", and hence contain only characters.

In official/standard ALGOL 68 only: <lang algol68>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)</lang>

E

<lang e><file:foo.txt>.getText()</lang>

The file is assumed to be in the default encoding.

J

<lang j> var=: 1!:1<'foo.txt'</lang>

To memory map the file:

<lang j> require'jmf'

  JCHAR map_jmf_ 'var';'foo.txt'</lang>

Caution: updating the value of the memory mapped variable will update the file, and this character remains when the variable's value is passed, unmodified, to a verb which modifies its own local variables.

PowerShell

<lang powershell>Get-Content foo.txt</lang> With explicit selection of encoding: <lang powershell>Get-Content foo.txt -Encoding UTF8</lang> However, both return an array of strings which is fine for pipeline use but if a single string is desired the array needs to be joined: <lang powershell>(Get-Content foo.txt) -join "`n"</lang>

Python

<lang python>open(filename).read()</lang>

This returns a byte string and does not assume any particular encoding.