Read entire file

From Rosetta Code
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.

Ada

Works with: Ada 2005

Using unbounded strings:

<lang Ada>with Ada.Strings.Unbounded, Ada.Text_IO;

procedure Whole_File is

  use Ada.Strings.Unbounded;
  Contents : Unbounded_String := Null_Unbounded_String;
  Filename : String := "whole_file.adb";
  File     : Ada.Text_IO.File_Type;

begin

  Ada.Text_IO.Open (File => File,
                    Mode => Ada.Text_IO.In_File,
                    Name => Filename);
  while not Ada.Text_IO.End_Of_File (File) loop
     Append (Contents, Ada.Text_IO.Get_Line (File));
     Append (Contents, ASCII.LF);
  end loop;
  -- remove last Linefeed
  Delete (Contents, Length(Contents), Length(Contents));
  Ada.Text_IO.Close (File);
  Ada.Text_IO.Put (To_String (Contents));

end Whole_File;</lang>

Using Ada.Directories to first ask for the file size:

<lang Ada>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;</lang>

Using Recursion:

<lang Ada>with Ada.Text_IO;

procedure Whole_File is

  Filename : String := "whole_file.adb";
  File     : Ada.Text_IO.File_Type;
  function File_To_String(File: Ada.Text_IO.File_Type) return String is
     Line : String := Ada.Text_IO.Get_Line(File);
  begin
     if Ada.Text_IO.End_Of_File (File) then
        return Line; -- no Line Feed at the end of the last line
     else
        return Line & ASCII.LF & File_To_String(File);
     end if;
  end File_To_String;

begin

  Ada.Text_IO.Open (File => File,
                    Mode => Ada.Text_IO.In_File,
                    Name => Filename);
  if not Ada.Text_IO.End_Of_File (File) then
     Ada.Text_IO.Put_Line(File_To_String(File));
  end if; -- do nothing if file is entirely empty

end Whole_File;</lang>

Using a memory map of the file:

Works with: POSIX

<lang Ada>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;</lang>

Character encodings and their handling are not really specified in Ada. What Ada does specify is three different character types (and corresponding string types):

  • Character - containing the set of ISO-8859-1 characters.
  • Wide_Character - containing the set of ISO-10646 BMP characters.
  • Wide_Wide_Character - containing the full set of ISO-10646 characters.

The GNU Ada compiler (GNAT) seems to read in text files as bytes, completely ignoring any operating system information on character encoding. You can use -gnatW8 in Ada 2005 mode to use UTF-8 characters in identifier names.

AutoHotkey

<lang AutoHotKey> fileread, varname, C:\filename.txt ; adding "MsgBox %varname%" (no quotes) to the next line will display the file contents.</lang> This script works fine as-is provided C:\filename.txt exists.

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>

BASIC

Whether or not various encodings are supported is implementation-specific.

Works with: QBasic

<lang qbasic>DIM f AS STRING OPEN "file.txt" FOR BINARY AS 1 f = SPACE$(LOF(1)) GET #1, 1, f CLOSE 1</lang>

BBC BASIC

In BBC BASIC for Windows and Brandy BASIC the maximum string length is 65535 characters. <lang bbcbasic> file% = OPENIN("input.txt")

     strvar$ = ""
     WHILE NOT EOF#file%
       strvar$ += CHR$(BGET#file%)
     ENDWHILE
     CLOSE #file%</lang>

API version:

<lang bbcbasic> file% = OPENIN("input.txt")

     strvar$ = STRING$(EXT#file%, " ")
     SYS "ReadFile", @hfile%(file%), !^strvar$, EXT#file%, ^temp%, 0
     CLOSE #file%</lang>

Brat

<lang brat>include :file

file.read file_name</lang>

C

It is not possible to specify encodings: the file is read as binary data (on some system, the b flag is ignored and there's no difference between "r" and "rb"; on others, it changes the way the "new lines" are treated, but this should not affect fread) <lang c>#include <stdio.h>

  1. 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;

}</lang>

Works with: POSIX

We can memory-map the file.

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <sys/mman.h>
  3. include <sys/types.h>
  4. include <sys/stat.h>
  5. include <unistd.h>
  6. 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;

}</lang>

C++

<lang Cpp>#include <iostream>

  1. include <fstream>
  2. include <string>
  3. 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 ;
  }

} </lang>

C#

Works with: C sharp version 3.0

<lang csharp>using System.IO;

class Program {

   static void Main(string[] args)
   {
       var fileContents = File.ReadAllText("c:\\autoexec.bat");
   }

} </lang>

Clojure

The core function slurp does the trick; you can specify an encoding as an optional second argument: <lang clojure>(slurp "myfile.txt") (slurp "my-utf8-file.txt" "UTF-8")</lang>

CMake

Sets a variable named string.

<lang cmake>file(READ /etc/passwd string)</lang>

This works with text files, but fails with binary files that contain NUL characters. CMake truncates the string at the first NUL character, and there is no way to detect this truncation.

The only way to read binary files is to use the HEX keyword to convert the entire file to a hexadecimal string.

<lang cmake>file(READ /etc/pwd.db string HEX)</lang>

D

D version 2. To read a whole file into a dynamic array of unsigned bytes: <lang d>import std.file: read;

void main() {

   auto data = cast(ubyte[])read("data.raw");

}</lang> To read a whole file into a validated UTF-8 string: <lang d>import std.file: readText;

void main() {

   string s = readText("text.txt");

}</lang>

Delphi

Using TStringList

<lang Delphi>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.</lang>


Works with: Delphi 2010 and above

<lang Delphi>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.</lang>

E

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

The file is assumed to be in the default encoding.

Euphoria

Euphoria cannot natively handle multibyte character encodings. The openEuphoria team is/was working on supporting it. It may have been implemented by now.

<lang euphoria> 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 </lang>

F#

<lang fsharp>// read entire file into variable using default system encoding or with specified encoding open System.IO let data = File.ReadAllText(filename) let utf8 = File.ReadAllText(filename, System.Text.Encoding.UTF8)</lang>

Factor

<lang factor>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</lang>

Fantom

Provide the filename to read from as a command-line parameter. <lang fantom> class ReadString {

 public static Void main (Str[] args)
 {
   Str contents := File(args[0].toUri).readAllStr
   echo ("contents: $contents")
 }

} </lang>

Forth

Works with: GNU Forth

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

Frink

The read[URL] function reads the entire contents of a URL. The encoding can be specified if necessary. <lang frink> a = read["file:yourfile.txt"] b = read["file:yourfile.txt", "UTF-8"] </lang>

GAP

<lang gap>InputTextFile("input.txt"); s := ReadAll(f);; # two semicolons to hide the result, which may be long CloseStream(f);</lang>

Go

Go has good support for working with strings as UTF-8, but there is no requirement that strings be UTF-8 and in fact they can hold arbitrary data. ioutil.ReadFile returns the contents of the file unaltered as a byte array. The conversion in the next line from byte array to string also makes no changes to the data. In the example below sv will have an exact copy of the data in the file, without regard to encoding. <lang go>import "io/ioutil"

data, err := ioutil.ReadFile(filename) sv := string(data)</lang> Go also supports memory mapped files through Linux mmap. The following prints the contents of "file". <lang go>package main

import (

   "fmt"
   "os"
   "syscall"

)

func main() {

   f, err := os.Open("file")
   if err != nil {
       fmt.Println(err)
       return
   }
   fi, err := f.Stat()
   if err != nil {
       fmt.Println(err)
       return
   }
   data, errno := syscall.Mmap(f.Fd(), 0, int(fi.Size),
       syscall.PROT_READ, syscall.MAP_PRIVATE)
   fmt.Println(errno, len(data))
   fmt.Println(string(data))

}</lang>

Groovy

<lang groovy>def fileContent = new File("c:\\file.txt").text</lang>

Haskell

In the IO monad:

<lang haskell>do text <- readFile filepath

  -- do stuff with text</lang>

Note that readFile is lazy. If you want to ensure the entire file is read in at once, before any other IO actions are run, try:

<lang haskell>eagerReadFile :: FilePath -> IO String eagerReadFile filepath = do

   text <- readFile filepath
   last text `seq` return text</lang>

Icon and Unicon

The first code snippet below reads from stdin directly into the string fs, preserving line separators (if any) and reading in large chunks. <lang Icon>every (fs := "") ||:= |reads(1000000)</lang> The second code snippet below performs the same operation using an intermediate list fL and applying a function (e.g. FUNC) to each line. Use this form when you need to perform additional string functions such as 'trim' or 'map' on each line. This avoids unnecessary garbage collections which will occur with larger files. The list can be discarded when done. Line separators are mapped into newlines. <lang Icon>every put(fL := [],|FUNC(read())) every (fs := "") ||:= !fL || "\n" fL := &null</lang>

Inform 7

File access is sandboxed by the interpreter, so this solution essentially requires that the file have been previously written by an Inform program running from the same location under the same interpreter.

<lang inform7>Home is a room.

The File of Testing is called "test".

When play begins: say "[text of the File of Testing]"; end the story.</lang>

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 characteristic 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 into a buffer. <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 {
       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();
   }

}</lang>

One can memory-map the file in Java, but there's little to gain if one is to create a String out of the file:

<lang java> 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; } }</lang>

JavaScript

This works in IExplorer or a standalone js file. Note the similarity to the VBScript code. <lang javascript>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)}</lang>

Liberty BASIC

<lang lb>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</lang>

Lua

<lang Lua> --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() </lang>

Mathematica

<lang Mathematica>Import["filename","String"]</lang>

Nimrod

<lang Nimrod>readFile(filename)</lang>

Objeck

<lang objeck> string := FileReader->ReadFile("in.txt"); </lang>

Objective-C

<lang objc>

   /*** 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);
   }

</lang>

OCaml

For most uses we can use this function:

<lang ocaml>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)</lang>

There is no problem reading an entire file with the function really_input because this function is implemented appropriately with an internal loop, but it can only load files which size is equal or inferior to the maximum length of an ocaml string. This maximum size is available with the variable Sys.max_string_length. On 32 bit machines this size is about 16Mo.

To load bigger files several solutions exist, for example create a structure that contains several strings where the contents of the file can be split. Or another solution that is often used is to use a bigarray of chars instead of a string:

<lang ocaml>type big_string =

 (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t</lang>

The function below returns the contents of a file with this type big_string, and it does so with "memory-mapping":

<lang ocaml>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)</lang>

Then the length of the data can be get with Bigarray.Array1.dim instead of String.length, and we can access to a given char with the syntactic sugar bstr.{i} (instead of str.[i]) as shown in the small piece of code below (similar to the cat command):

<lang ocaml>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</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.

PARI/GP

GP's ability to read files is extremely limited; reading an entire file is almost all that it can do. PARI is not similarly limited. <lang parigp>text=read("file.txt");</lang>

Perl

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

Perl 6

Works with: Rakudo version 2010.07

<lang perl6>my $string = slurp 'sample.txt';</lang>

PHP

<lang php>file_get_contents($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.

Pike

<lang pike>string content=Stdio.File("foo.txt")->read();</lang>

PL/I

<lang PL/I> get file (in) edit ((substr(s, i, 1) do i = 1 to 32767)) (a); </lang>

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>

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.

In Python 3 strings are in unicode, you can specify encoding when reading:

<lang python>open(filename, encoding='utf-8').read()</lang>

R

<lang r>fname <- "notes.txt" len <- file.info(fname)$size conn <- file(fname, 'r') contents <- readChar(conn, len) close(conn)</lang>

REALbasic

This function accepts a file (FolderItem object) and an optional TextEncoding class. If the TextEncoding is not defined, then REALbasic defaults to UTF-8. Since it is intended for cross-platform development, REALbasic has a number of built-in tools for working with different text encodings, line terminators, etc. [1] <lang realbasic> 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 </lang>

REBOL

<lang rebol>read %my-file  ; read as text read/binary %my-file  ; preserve contents exactly</lang>

Retro

<lang Retro>with files' here "input.txt" slurp</lang>

REXX

<lang rexx> /*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

</lang>

Ruby

<lang ruby># Read entire file. str = IO.read "foobar.txt"</lang>

<lang ruby># It can also read a subprocess. str = IO.read "| grep afs3 /etc/services"</lang>

Caution! IO.read and File.read take a portname. To open an arbitrary filename (which might start with "|"), you must use File.open, then IO#read.

<lang ruby>filename = "|strange-name.txt" str = File.open(filename) {|f| f.read}</lang>

Seed7

The getf.s7i library supports the function getf, which reads a whole file into a string:

$ include "seed7_05.s7i";
  include "getf.s7i";

const proc: main is func
  local
    var string: fileContent is "";
  begin
    fileContent := getf("text.txt");
  end func;

Smalltalk

Works with: Pharo

<lang smalltalk> (StandardFileStream oldFileNamed: 'foo.txt') contents. </lang>

SNOBOL4

In SNOBOL4, file I/O is done by associating a variable with the desired file, via the input() built-in function. After the association, each reference to the named variable provides as the variable's value the next block or line of data from the corresponding file. The exact format of the input() function parameters tends to vary based on the implementation in use. In this example, the code reads the file in blocks of 512k bytes (or less) until the entire file has been read into one long string in memory.

<lang SNOBOL4> input(.inbin,21,"filename.txt [-r524288]") :f(end) rdlp buf = inbin :s(rdlp)

  • now process the 'buf' containing the file

end</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>

TUSCRIPT

<lang tuscript> $$ MODE TUSCRIPT ERROR/STOP OPEN ("rosetta.txt",READ,-std-) var=FILE ("rosetta.txt") </lang>

TXR

<lang txr>#!/usr/bin/txr @(next "foo.txt") @(freeform) @LINE </lang>

The freeform directive in TXR causes the remaining lines of the text stream to be treated as one big line, catenated together. The default line terminator is the newline "\n". This lets the entire input be captured into a single variable as a whole-line match.

UNIX Shell

We start a 'cat' process to read the entire file, and use '$(...)' to grab the output of 'cat'. We use 'printf' which might be more portable than 'echo'. Because '$(...)' can chop off a newline at the end of the file, we tell 'printf' to add an extra newline.

<lang bash>f=$(cat foo.txt) printf '%s\n' "$f"</lang>

Some shells provide a shortcut to read a file without starting a 'cat' process.

Works with: bash
Works with: pdksh

<lang bash>f=$(<foo.txt) echo -E "$f"</lang>

VBScript

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

Read text file with UTF-16 encoding into memory and display <lang vb>wscript.echo createobject("scripting.filesystemobject").opentextfile("utf16encoded.txt",1,-1).readall</lang>

Visual Basic .NET

<lang vbnet>Imports System.IO

Public Class Form1

 ' Read all of the lines of a file.
 ' Function assumes that the file exists.
 Private Sub ReadLines(ByVal FileName As String)
   Dim oReader As New StreamReader(FileName)
   Dim sLine As String = oReader.ReadToEnd()
   oReader.Close()
 End Sub

End Class</lang>

Yorick

This loads foo.txt into lines as an array of strings. Each array element is one line. Each line's trailing newline is removed. <lang yorick>lines = rdfile("foo.txt");</lang> This loads foo.txt into content as a single scalar string, without losing newlines. <lang yorick>f = open("foo.txt", "rb"); raw = array(char, sizeof(f)); _read, f, 0, raw; close, f; content = strchar(raw);</lang>

Zsh

<lang zsh>file=$(<foo.txt) print $file</lang> alternatively <lang zsh>zmodload zsh/mapfile print $mapfile[foo.txt]</lang>