Read

Read[stream]

reads one expression from an input stream and returns the expression.

Read[stream,type]

reads one object of the specified type.

Read[stream,{type1,type2,}]

reads a sequence of objects of the specified types.

Details and Options

  • Possible types to read are:
  • Bytesingle byte, returned as an integer code
    Charactersingle character, returned as a onecharacter string
    Expressioncomplete Wolfram Language expression
    Numberinteger or an approximate number, given in "E" format
    Realapproximate number, given in "E" format
    Recordsequence of characters delimited by record separators
    Stringstring terminated by a newline
    Wordsequence of characters delimited by word separators
  • Objects of type Real can be given in the scientific notation format used by languages such as C and Fortran, as well as in standard Wolfram Language format. A form like 2.e5 or 2.E5 as well as 2*^5 can be used to represent the number . Objects read as type Real are always returned as approximate numbers. Objects read as type Number are returned as integers if they contain no explicit decimal points.
  • The following options can be given:
  • NullRecordsFalsewhether to assume a null record between repeated record separators
    NullWordsFalsewhether to assume a null word between repeated word separators
    RecordSeparators{"\r\n","\n","\r"}separators allowed between records
    TokenWords{}words taken as delimiters
    WordSeparators{" ","t"}separators allowed between words
  • Objects of type String must be terminated by newlines.
  • You can specify any nested list of types for Read to look for. Each successive object read will be placed in the next position in the list structure. A depthfirst traversal of the list structure is used.
  • Read[stream,{Number,Number}] reads a pair of numbers from an input stream, and gives the result as a twoelement list.
  • Read[stream,{{Number,Number},{Number,Number}}] reads a 2×2 matrix, going through each column, then each row.
  • You can use Read to get objects to insert into any expression structure, not necessarily a list. Example: Read[stream,Hold[Expression]] gets an expression and places it inside Hold.
  • The first argument to Read can be InputStream["name",n], or simply "name" if there is at most one open input stream with the specified name. Read will call OpenRead to open files with no associated stream.
  • Read[File["file"],] is also supported.
  • You can open a file or pipe to get an InputStream object using OpenRead.
  • There is always a "current point" maintained for any stream. When you read an object from a stream, the current point is left after the input you read. Successive calls to Read can therefore be used to read successive objects in a stream such as a file.
  • Read returns EndOfFile for each object you try to read after you have reached the end of a file.
  • Read returns $Failed if it cannot read an object of the type you requested.
  • If there is a syntax error in a Wolfram Language expression that you try to read, then Read leaves the current point at the position of the error, and returns $Failed.

Examples

open allclose all

Basic Examples  (2)

Open a stream:

Read expressions from the stream:

Open a stream:

Read numbers from the stream:

Read words and numbers:

Close the stream:

Scope  (1)

Read an expression from the file specified by the File object:

Possible Issues  (1)

Some streams, like those created by StringToStream, give values greater than 255 for the Byte type:

These correspond to the individual character codes:

Close the stream:

Wolfram Research (1988), Read, Wolfram Language function, https://reference.wolfram.com/language/ref/Read.html (updated 2016).

Text

Wolfram Research (1988), Read, Wolfram Language function, https://reference.wolfram.com/language/ref/Read.html (updated 2016).

CMS

Wolfram Language. 1988. "Read." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/Read.html.

APA

Wolfram Language. (1988). Read. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Read.html

BibTeX

@misc{reference.wolfram_2024_read, author="Wolfram Research", title="{Read}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/Read.html}", note=[Accessed: 21-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_read, organization={Wolfram Research}, title={Read}, year={2016}, url={https://reference.wolfram.com/language/ref/Read.html}, note=[Accessed: 21-November-2024 ]}