Read entire file: Difference between revisions

From Rosetta Code
Content added Content deleted
(added PHP)
Line 53: Line 53:
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:
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>
<lang powershell>(Get-Content foo.txt) -join "`n"</lang>

=={{header|PHP}}==
<lang php>file_get_contents($filename)</lang>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 04:18, 30 June 2010

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

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.

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>

Once a "book" has been read into a book array it can still be associated with a virtual file and again be accessed with standard file routines (such as readf, printf, putf, getf, new line etc). This means data can be directly manipulated from a array cached in "core" using transput (stdio) routines.

In official/standard ALGOL 68 only: <lang algol68>FILE cached book file; associate(cached 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>

PHP

<lang php>file_get_contents($filename)</lang>

Python

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

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