Read entire file

From Rosetta Code
Revision as of 06:14, 9 July 2010 by 202.72.158.213 (talk) (VBScript)
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.

The book can contain new pages and new lines, are not of any particular character set, hence are system independent. The character set is set by a call to make conv, eg make conv(tape, ebcdic conv); - c.f. Character_codes for more details.

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.

Forth

Works with: GNU Forth

<lang forth>s" foo.txt" slurp-file ( str len )</lang>

Haskell

in the IO monad: <lang haskell>do text <- readFile filename

  -- do stuff with text</lang>

Icon

The first code snippet below reads from stdin directly into the string fs. <lang Icon>every (fs := "") ||:= |read()</lang> The second code snippet below performs the same operation using an intermediate list fL and applying a function to each line. Use this construction if you create any intermediate strings to avoid unnecessary garbage collections from FUNC (examples would be procedures like trim() or map()) <lang Icon>every put(fL := [],|FUNC(read())) every (fs := "") ||:= !fL</lang>

Unicon

This Icon solution works in Unicon.

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.

Java

There is no single method to do this in Java (probably because reading an entire file at once could fill up your memory quickly), so to do this you could simply append the contents as you read them line-by-line as in File IO. <lang java>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 {
       BufferedReader in = new BufferedReader(new FileReader(filename));
       StringBuilder contents = new StringBuilder();
       while(in.ready()){
           contents.append(in.readLine() + "\n");
       }
       return contents.toString();
   }

}</lang>

Oz

The interface for file operations is object-oriented. <lang oz>declare

 FileHandle = {New Open.file init(name:"test.txt")}
 FileContents = {FileHandle read(size:all list:$)}

in

 {FileHandle close}
 {System.printInfo FileContents}</lang>

FileContents is a list of bytes. The operation does not assume any particular encoding.

Perl

<lang perl>open my $fh, $filename; my $text = do { local( $/ ); <$fh> };</lang> or <lang perl>use File::Slurp; my $text = read_file($filename);</lang>

PicoLisp

Using 'till' is the shortest way: <lang PicoLisp>(in "file" (till NIL T))</lang> To read the file into a list of characters: <lang PicoLisp>(in "file" (till NIL))</lang> or, more explicit: <lang PicoLisp>(in "file" (make (while (char) (link @))))</lang> Encoding is always assumed to be UTF-8.

PowerShell

<lang powershell>Get-Content foo.txt</lang> This will only detect Unicode correctly with a BOM in place (even for UTF-8). 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>

PureBasic

A file can be read with any of the built in commands <lang PureBasic>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)</lang> If the file is s pure text file (no CR/LF etc.), this will work and will read each line untill EOL is found. <lang PureBasic>If ReadFile(0, "RC.txt")

 Variable$=ReadString(0)     
 CloseFile(0) 

EndIf</lang> Since PureBasic terminates strings with a #NULL and also split the ReadString() is encountering new line chars, any file containing these must be treated as a data stream. <lang PureBasic>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</lang>

Python

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

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

Ruby

<lang ruby>IO.read(filename)</lang>

Tcl

This reads the data in as text, applying the default encoding translations. <lang tcl>set f [open $filename] set data [read $f] close $f</lang> To read the data in as uninterpreted bytes, either use fconfigure to put the handle into binary mode before reading, or (from Tcl 8.5 onwards) do this: <lang tcl>set f [open $filename "rb"] set data [read $f] close $f</lang>

VBScript

Read text file with default encoding into variable and display (would do for quine task as well) <lang vb>dim s s = createobject("scripting.filesystemobject").opentextfile("slurp.vbs",1).readall wscript.echo s</lang>

Read text file with UTF16 encoding into memory and display (would do for quine task as well) <lang vb>wscript.echo createobject("scripting.filesystemobject").opentextfile("slurp.vbs",1,-1).readall</lang>