| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Octave's C-style input and output functions provide most of the functionality of the C programming language's standard I/O library. The argument lists for some of the input functions are slightly different, however, because Octave has no way of passing arguments by reference.
In the following, file refers to a file name and fid refers
to an integer file number, as returned by fopen.
There are three files that are always available. Although these files can be accessed using their corresponding numeric file ids, you should always use the symbolic names given in the table below, since it will make your programs easier to understand.
Return the numeric value corresponding to the standard input stream.
When Octave is used interactively, this is filtered through the command
line editing functions.
See also: stdout, stderr.
Return the numeric value corresponding to the standard output stream.
Data written to the standard output is normally filtered through the pager.
See also: stdin, stderr.
Return the numeric value corresponding to the standard error stream.
Even if paging is turned on, the standard error is not sent to the
pager. It is useful for error messages and prompts.
See also: stdin, stdout.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When reading data from a file it must be opened for reading first, and
likewise when writing to a file. The fopen function returns a
pointer to an open file that is ready to be read or written. Once all
data has been read from or written to the opened file it should be closed.
The fclose function does this. The following code illustrates
the basic pattern for writing to a file, but a very similar pattern is
used when reading a file.
filename = "myfile.txt"; fid = fopen (filename, "w"); # Do the actual I/O here... fclose (fid); |
The first form of the fopen function opens the named file with
the specified mode (read-write, read-only, etc.) and architecture
interpretation (IEEE big endian, IEEE little endian, etc.), and returns
an integer value that may be used to refer to the file later. If an
error occurs, fid is set to -1 and msg contains the
corresponding system error message. The mode is a one or two
character string that specifies whether the file is to be opened for
reading, writing, or both.
The second form of the fopen function returns a vector of file ids
corresponding to all the currently open files, excluding the
stdin, stdout, and stderr streams.
The third form of the fopen function returns information about the
open file given its file id.
For example,
myfile = fopen ("splat.dat", "r", "ieee-le");
|
opens the file `splat.dat' for reading. If necessary, binary numeric values will be read assuming they are stored in IEEE format with the least significant bit first, and then converted to the native representation.
Opening a file that is already open simply opens it again and returns a separate file id. It is not an error to open a file several times, though writing to the same file through several different file ids may produce unexpected results.
The possible values `mode' may have are
Open a file for reading.
Open a file for writing. The previous contents are discarded.
Open or create a file for writing at the end of the file.
Open an existing file for reading and writing.
Open a file for reading or writing. The previous contents are discarded.
Open or create a file for reading or writing at the end of the file.
Append a "t" to the mode string to open the file in text mode or a "b" to open in binary mode. On Windows and Macintosh systems, text mode reading and writing automatically converts linefeeds to the appropriate line end character for the system (carriage-return linefeed on Windows, carriage-return on Macintosh). The default if no mode is specified is binary mode.
Additionally, you may append a "z" to the mode string to open a gzipped file for reading or writing. For this to be successful, you must also open the file in binary mode.
The parameter arch is a string specifying the default data format for the file. Valid values for arch are:
`native' The format of the current machine (this is the default).
`ieee-be' IEEE big endian format.
`ieee-le' IEEE little endian format.
`vaxd' VAX D floating format.
`vaxg' VAX G floating format.
`cray' Cray floating format.
however, conversions are currently only supported for `native'
`ieee-be', and `ieee-le' formats.
See also: fclose, fread, fseek.
Closes the specified file. If successful, fclose returns 0,
otherwise, it returns -1.
See also: fopen, fseek, ftell.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Once a file has been opened for writing a string can be written to the
file using the fputs function. The following example shows
how to write the string `Free Software is needed for Free Science'
to the file `free.txt'.
filename = "free.txt"; fid = fopen (filename, "w"); fputs (fid, "Free Software is needed for Free Science"); fclose (fid); |
Write a string to a file with no formatting.
Return a non-negative number on success and EOF on error.
A function much similar to fputs is available for writing data
to the screen. The puts function works just like fputs
except it doesn't take a file pointer as its input.
Write a string to the standard output with no formatting.
Return a non-negative number on success and EOF on error.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To read from a file it must be opened for reading using fopen.
Then a line can be read from the file using fgetl as the following
code illustrates
fid = fopen ("free.txt");
txt = fgetl (fid)
-| Free Software is needed for Free Science
fclose (fid);
|
This of course assumes that the file `free.txt' exists and contains the line `Free Software is needed for Free Science'.
Read characters from a file, stopping after a newline, or EOF, or len characters have been read. The characters read, excluding the possible trailing newline, are returned as a string.
If len is omitted, fgetl reads until the next newline
character.
If there are no more characters to read, fgetl returns -1.
See also: fread, fscanf.
Read characters from a file, stopping after a newline, or EOF, or len characters have been read. The characters read, including the possible trailing newline, are returned as a string.
If len is omitted, fgets reads until the next newline
character.
If there are no more characters to read, fgets returns -1.
See also: fread, fscanf.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes how to call printf and related functions.
The following functions are available for formatted output. They are modelled after the C language functions of the same name, but they interpret the format template differently in order to improve the performance of printing vector and matrix values.
Print optional arguments under the control of the template string
template to the stream stdout and return the number of
characters printed.
See also: fprintf, sprintf, scanf.
This function is just like printf, except that the output is
written to the stream fid instead of stdout.
See also: printf, sprintf, fread, fscanf, fopen, fclose.
This is like printf, except that the output is returned as a
string. Unlike the C library function, which requires you to provide a
suitably sized string as an argument, Octave's sprintf function
returns the string, automatically sized to hold all of the items
converted.
See also: printf, fprintf, sscanf.
The printf function can be used to print any number of arguments.
The template string argument you supply in a call provides
information not only about the number of additional arguments, but also
about their types and what style should be used for printing them.
Ordinary characters in the template string are simply written to the output stream as-is, while conversion specifications introduced by a `%' character in the template cause subsequent arguments to be formatted and written to the output stream. For example,
pct = 37;
filename = "foo.txt";
printf ("Processed %d%% of `%s'.\nPlease be patient.\n",
pct, filename);
|
produces output like
Processed 37% of `foo.txt'. Please be patient. |
This example shows the use of the `%d' conversion to specify that a scalar argument should be printed in decimal notation, the `%s' conversion to specify printing of a string argument, and the `%%' conversion to print a literal `%' character.
There are also conversions for printing an integer argument as an unsigned value in octal, decimal, or hexadecimal radix (`%o', `%u', or `%x', respectively); or as a character value (`%c').
Floating-point numbers can be printed in normal, fixed-point notation using the `%f' conversion or in exponential notation using the `%e' conversion. The `%g' conversion uses either `%e' or `%f' format, depending on what is more appropriate for the magnitude of the particular number.
You can control formatting more precisely by writing modifiers between the `%' and the character that indicates which conversion to apply. These slightly alter the ordinary behavior of the conversion. For example, most conversion specifications permit you to specify a minimum field width and a flag indicating whether you want the result left- or right-justified within the field.
The specific flags and modifiers that are permitted and their interpretation vary depending on the particular conversion. They're all described in more detail in the following sections.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When given a matrix value, Octave's formatted output functions cycle through the format template until all the values in the matrix have been printed. For example,
printf ("%4.2f %10.2e %8.4g\n", hilb (3));
-| 1.00 5.00e-01 0.3333
-| 0.50 3.33e-01 0.25
-| 0.33 2.50e-01 0.2
|
If more than one value is to be printed in a single call, the output functions do not return to the beginning of the format template when moving on from one value to the next. This can lead to confusing output if the number of elements in the matrices are not exact multiples of the number of conversions in the format template. For example,
printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]);
-| 1.00 2.00e+00 3
-| 4.00
|
If this is not what you want, use a series of calls instead of just one.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section provides details about the precise syntax of conversion
specifications that can appear in a printf template
string.
Characters in the template string that are not part of a conversion specification are printed as-is to the output stream.
The conversion specifications in a printf template string have
the general form:
% flags width [ . precision ] type conversion |
For example, in the conversion specifier `%-10.8ld', the `-' is a flag, `10' specifies the field width, the precision is `8', the letter `l' is a type modifier, and `d' specifies the conversion style. (This particular type specifier says to print a numeric argument in decimal notation, with a minimum of 8 digits left-justified in a field at least 10 characters wide.)
In more detail, output conversion specifications consist of an initial `%' character followed in sequence by:
You can also specify a field width of `*'. This means that the next argument in the argument list (before the actual value to be printed) is used as the field width. The value is rounded to the nearest integer. If the value is negative, this means to set the `-' flag (see below) and to use the absolute value as the field width.
You can also specify a precision of `*'. This means that the next argument in the argument list (before the actual value to be printed) is used as the precision. The value must be an integer, and is ignored if it is negative.
printf function, but is recognized to provide
compatibility with the C language printf.
The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they use.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is a table summarizing what all the different conversions do:
Print an integer as a signed decimal number. See section Integer Conversions, for details. `%d' and `%i' are synonymous for
output, but are different when used with scanf for input
(see section Table of Input Conversions).
Print an integer as an unsigned octal number. See section Integer Conversions, for details.
Print an integer as an unsigned decimal number. See section Integer Conversions, for details.
Print an integer as an unsigned hexadecimal number. `%x' uses lower-case letters and `%X' uses upper-case. See section Integer Conversions, for details.
Print a floating-point number in normal (fixed-point) notation. See section Floating-Point Conversions, for details.
Print a floating-point number in exponential notation. `%e' uses lower-case letters and `%E' uses upper-case. See section Floating-Point Conversions, for details.
Print a floating-point number in either normal (fixed-point) or exponential notation, whichever is more appropriate for its magnitude. `%g' uses lower-case letters and `%G' uses upper-case. See section Floating-Point Conversions, for details.
Print a single character. See section Other Output Conversions.
Print a string. See section Other Output Conversions.
Print a literal `%' character. See section Other Output Conversions.
If the syntax of a conversion specification is invalid, unpredictable things will happen, so don't do this. If there aren't enough function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are unpredictable. If you supply more arguments than conversion specifications, the extra argument values are simply ignored; this is sometimes useful.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the options for the `%d', `%i', `%o', `%u', `%x', and `%X' conversion specifications. These conversions print integers in various formats.
The `%d' and `%i' conversion specifications both print an numeric argument as a signed decimal number; while `%o', `%u', and `%x' print the argument as an unsigned octal, decimal, or hexadecimal number (respectively). The `%X' conversion specification is just like `%x' except that it uses the characters `ABCDEF' as digits instead of `abcdef'.
The following flags are meaningful:
Left-justify the result in the field (instead of the normal right-justification).
For the signed `%d' and `%i' conversions, print a plus sign if the value is positive.
For the signed `%d' and `%i' conversions, if the result doesn't start with a plus or minus sign, prefix it with a space character instead. Since the `+' flag ensures that the result includes a sign, this flag is ignored if you supply both of them.
For the `%o' conversion, this forces the leading digit to be `0', as if by increasing the precision. For `%x' or `%X', this prefixes a leading `0x' or `0X' (respectively) to the result. This doesn't do anything useful for the `%d', `%i', or `%u' conversions.
Pad the field with zeros instead of spaces. The zeros are placed after any indication of sign or base. This flag is ignored if the `-' flag is also specified, or if a precision is specified.
If a precision is supplied, it specifies the minimum number of digits to appear; leading zeros are produced if necessary. If you don't specify a precision, the number is printed with as many digits as it needs. If you convert a value of zero with an explicit precision of zero, then no characters at all are produced.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section discusses the conversion specifications for floating-point numbers: the `%f', `%e', `%E', `%g', and `%G' conversions.
The `%f' conversion prints its argument in fixed-point notation,
producing output of the form
[-]ddd.ddd,
where the number of digits following the decimal point is controlled
by the precision you specify.
The `%e' conversion prints its argument in exponential notation,
producing output of the form
[-]d.ddde[+|-]dd.
Again, the number of digits following the decimal point is controlled by
the precision. The exponent always contains at least two digits. The
`%E' conversion is similar but the exponent is marked with the letter
`E' instead of `e'.
The `%g' and `%G' conversions print the argument in the style of `%e' or `%E' (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use the `%f' style. Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.
The following flags can be used to modify the behavior:
Left-justify the result in the field. Normally the result is right-justified.
Always include a plus or minus sign in the result.
If the result doesn't start with a plus or minus sign, prefix it with a space instead. Since the `+' flag ensures that the result includes a sign, this flag is ignored if you supply both of them.
Specifies that the result should always include a decimal point, even if no digits follow it. For the `%g' and `%G' conversions, this also forces trailing zeros after the decimal point to be left in place where they would otherwise be removed.
Pad the field with zeros instead of spaces; the zeros are placed after any sign. This flag is ignored if the `-' flag is also specified.
The precision specifies how many digits follow the decimal-point
character for the `%f', `%e', and `%E' conversions. For
these conversions, the default precision is 6. If the precision
is explicitly 0, this suppresses the decimal point character
entirely. For the `%g' and `%G' conversions, the precision
specifies how many significant digits to print. Significant digits are
the first digit before the decimal point, and all the digits after it.
If the precision is 0 or not specified for `%g' or
`%G', it is treated like a value of 1. If the value being
printed cannot be expressed precisely in the specified number of digits,
the value is rounded to the nearest number that fits.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes miscellaneous conversions for printf.
The `%c' conversion prints a single character. The `-' flag can be used to specify left-justification in the field, but no other flags are defined, and no precision or type modifier can be given. For example:
printf ("%c%c%c%c%c", "h", "e", "l", "l", "o");
|
prints `hello'.
The `%s' conversion prints a string. The corresponding argument must be a string. A precision can be specified to indicate the maximum number of characters to write; otherwise characters in the string up to but not including the terminating null character are written to the output stream. The `-' flag can be used to specify left-justification in the field, but no other flags or type modifiers are defined for this conversion. For example:
printf ("%3s%-6s", "no", "where");
|
prints ` nowhere ' (note the leading and trailing spaces).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Octave provides the scanf, fscanf, and sscanf
functions to read formatted input. There are two forms of each of these
functions. One can be used to extract vectors of data from a file, and
the other is more `C-like'.
In the first form, read from fid according to template, returning the result in the matrix val.
The optional argument size specifies the amount of data to read and may be one of
InfRead as much as possible, returning a column vector.
nrRead up to nr elements, returning a column vector.
[nr, Inf]Read as much as possible, returning a matrix with nr rows. If the number of elements read is not an exact multiple of nr, the last column is padded with zeros.
[nr, nc]Read up to nr * nc elements, returning a matrix with
nr rows. If the number of elements read is not an exact multiple
of nr, the last column is padded with zeros.
If size is omitted, a value of Inf is assumed.
A string is returned if template specifies only character conversions.
The number of items successfully read is returned in count.
In the second form, read from fid according to template,
with each conversion specifier in template corresponding to a
single scalar return value. This form is more `C-like', and also
compatible with previous versions of Octave. The number of successful
conversions is returned in count
See also: scanf, sscanf, fread, fprintf.
This is like fscanf, except that the characters are taken from the
string string instead of from a stream. Reaching the end of the
string is treated as an end-of-file condition.
See also: fscanf, scanf, sprintf.
Calls to scanf are superficially similar to calls to
printf in that arbitrary arguments are read under the control of
a template string. While the syntax of the conversion specifications in
the template is very similar to that for printf, the
interpretation of the template is oriented more towards free-format
input and simple pattern matching, rather than fixed-field formatting.
For example, most scanf conversions skip over any amount of
"white space" (including spaces, tabs, and newlines) in the input
file, and there is no concept of precision for the numeric input
conversions as there is for the corresponding output conversions.
Ordinarily, non-whitespace characters in the template are expected to
match characters in the input stream exactly.
When a matching failure occurs, scanf returns immediately,
leaving the first non-matching character as the next character to be
read from the stream, and scanf returns all the items that were
successfully converted.
The formatted input functions are not used as frequently as the formatted output functions. Partly, this is because it takes some care to use them properly. Another reason is that it is difficult to recover from a matching error.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A scanf template string is a string that contains ordinary
multibyte characters interspersed with conversion specifications that
start with `%'.
Any whitespace character in the template causes any number of whitespace characters in the input stream to be read and discarded. The whitespace characters that are matched need not be exactly the same whitespace characters that appear in the template string. For example, write ` , ' in the template to recognize a comma with optional whitespace before and after.
Other characters in the template string that are not part of conversion specifications must match characters in the input stream exactly; if this is not the case, a matching failure occurs.
The conversion specifications in a scanf template string
have the general form:
% flags width type conversion |
In more detail, an input conversion specification consists of an initial `%' character followed in sequence by:
scanf finds a conversion
specification that uses this flag, it reads input as directed by the
rest of the conversion specification, but it discards this input, does
not return any value, and does not increment the count of
successful assignments.
scanf function, but is recognized to provide
compatibility with the C language scanf.
The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they allow.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is a table that summarizes the various conversion specifications:
Matches an optionally signed integer written in decimal. See section Numeric Input Conversions.
Matches an optionally signed integer in any of the formats that the C language defines for specifying an integer constant. See section Numeric Input Conversions.
Matches an unsigned integer written in octal radix. See section Numeric Input Conversions.
Matches an unsigned integer written in decimal radix. See section Numeric Input Conversions.
Matches an unsigned integer written in hexadecimal radix. See section Numeric Input Conversions.
Matches an optionally signed floating-point number. See section Numeric Input Conversions.
Matches a string containing only non-whitespace characters. See section String Input Conversions.
Matches a string of one or more characters; the number of characters read is controlled by the maximum field width given for the conversion. See section String Input Conversions.
This matches a literal `%' character in the input stream. No corresponding argument is used.
If the syntax of a conversion specification is invalid, the behavior is undefined. If there aren't enough function arguments provided to supply addresses for all the conversion specifications in the template strings that perform assignments, or if the arguments are not of the correct types, the behavior is also undefined. On the other hand, extra arguments are simply ignored.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the scanf conversions for reading numeric
values.
The `%d' conversion matches an optionally signed integer in decimal radix.
The `%i' conversion matches an optionally signed integer in any of the formats that the C language defines for specifying an integer constant.
For example, any of the strings `10', `0xa', or `012'
could be read in as integers under the `%i' conversion. Each of
these specifies a number with decimal value 10.
The `%o', `%u', and `%x' conversions match unsigned integers in octal, decimal, and hexadecimal radices, respectively.
The `%X' conversion is identical to the `%x' conversion. They both permit either uppercase or lowercase letters to be used as digits.
Unlike the C language scanf, Octave ignores the `h',
`l', and `L' modifiers.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the scanf input conversions for reading
string and character values: `%s' and `%c'.
The `%c' conversion is the simplest: it matches a fixed number of characters, always. The maximum field with says how many characters to read; if you don't specify the maximum, the default is 1. This conversion does not skip over initial whitespace characters. It reads precisely the next n characters, and fails if it cannot get that many.
The `%s' conversion matches a string of non-whitespace characters. It skips and discards initial whitespace, but stops when it encounters more whitespace after having read something.
For example, reading the input:
hello, world |
with the conversion `%10c' produces " hello, wo", but
reading the same input with the conversion `%10s' produces
"hello,".
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Octave can read and write binary data using the functions fread
and fwrite, which are patterned after the standard C functions
with the same names. They are able to automatically swap the byte order
of integer data and convert among the supported floating point formats
as the data are read.
Read binary data of type precision from the specified file ID fid.
The optional argument size specifies the amount of data to read and may be one of
InfRead as much as possible, returning a column vector.
nrRead up to nr elements, returning a column vector.
[nr, Inf]Read as much as possible, returning a matrix with nr rows. If the number of elements read is not an exact multiple of nr, the last column is padded with zeros.
[nr, nc]Read up to nr * nc elements, returning a matrix with
nr rows. If the number of elements read is not an exact multiple
of nr, the last column is padded with zeros.
If size is omitted, a value of Inf is assumed.
The optional argument precision is a string specifying the type of data to read and may be one of
"schar""signed char"Signed character.
"uchar""unsigned char"Unsigned character.
"int8""integer*1"8-bit signed integer.
"int16""integer*2"16-bit signed integer.
"int32""integer*4"32-bit signed integer.
"int64""integer*8"64-bit signed integer.
"uint8"8-bit unsigned integer.
"uint16"16-bit unsigned integer.
"uint32"32-bit unsigned integer.
"uint64"64-bit unsigned integer.
"single""float32""real*4"32-bit floating point number.
"double""float64""real*8"64-bit floating point number.
"char""char*1"Single character.
"short"Short integer (size is platform dependent).
"int"Integer (size is platform dependent).
"long"Long integer (size is platform dependent).
"ushort""unsigned short"Unsigned short integer (size is platform dependent).
"uint""unsigned int"Unsigned integer (size is platform dependent).
"ulong""unsigned long"Unsigned long integer (size is platform dependent).
"float"Single precision floating point number (size is platform dependent).
The default precision is "uchar".
The precision argument may also specify an optional repeat
count. For example, `32*single' causes fread to read
a block of 32 single precision floating point numbers. Reading in
blocks is useful in combination with the skip argument.
The precision argument may also specify a type conversion.
For example, `int16=>int32' causes fread to read 16-bit
integer values and return an array of 32-bit integer values. By
default, fread returns a double precision array. The special
form `*TYPE' is shorthand for `TYPE=>TYPE'.
The conversion and repeat counts may be combined. For example, the
specification `32*single=>single' causes fread to read
blocks of single precision floating point values and return an array
of single precision values instead of the default array of double
precision values.
The optional argument skip specifies the number of bytes to skip after each element (or block of elements) is read. If it is not specified, a value of 0 is assumed. If the final block read is not complete, the final skip is omitted. For example,
fread (f, 10, "3*single=>single", 8) |
will omit the final 8-byte skip because the last read will not be a complete block of 3 values.
The optional argument arch is a string specifying the data format for the file. Valid values are
"native"The format of the current machine.
"ieee-be"IEEE big endian.
"ieee-le"IEEE little endian.
"vaxd"VAX D floating format.
"vaxg"VAX G floating format.
"cray"Cray floating format.
Conversions are currently only supported for "ieee-be" and
"ieee-le" formats.
The data read from the file is returned in val, and the number of
values read is returned in count
See also: fwrite, fopen, fclose.
Write data in binary form of type precision to the specified file ID fid, returning the number of values successfully written to the file.
The argument data is a matrix of values that are to be written to the file. The values are extracted in column-major order.
The remaining arguments precision, skip, and arch are
optional, and are interpreted as described for fread.
The behavior of fwrite is undefined if the values in data
are too large to fit in the specified precision.
See also: fread, fopen, fclose.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Sometimes one needs to write data to a file that is only temporary. This is most commonly used when an external program launched from within Octave needs to access data. When Octave exits all temporary files will be deleted, so this step need not be executed manually.
Return the file ID corresponding to a new temporary file with a unique
name created from template. The last six characters of template
must be XXXXXX and these are replaced with a string that makes the
filename unique. The file is then created with mode read/write and
permissions that are system dependent (on GNU/Linux systems, the permissions
will be 0600 for versions of glibc 2.0.7 and later). The file is opened
with the O_EXCL flag.
If the optional argument delete is supplied and is true,
the file will be deleted automatically when Octave exits, or when
the function purge_tmp_files is called.
If successful, fid is a valid file ID, name is the name of
the file, and msg is an empty string. Otherwise, fid
is -1, name is empty, and msg contains a system-dependent
error message.
See also: tmpfile, tmpnam, P_tmpdir.
Return the file ID corresponding to a new temporary file with a unique
name. The file is opened in binary read/write ("w+b") mode.
The file will be deleted automatically when it is closed or when Octave
exits.
If successful, fid is a valid file ID and msg is an empty
string. Otherwise, fid is -1 and msg contains a
system-dependent error message.
See also: tmpnam, mkstemp, P_tmpdir.
Return a unique temporary file name as a string.
If prefix is omitted, a value of "oct-" is used.
If dir is also omitted, the default directory for temporary files
is used. If dir is provided, it must exist, otherwise the default
directory for temporary files is used. Since the named file is not
opened, by tmpnam, it is possible (though relatively unlikely)
that it will not be available by the time your program attempts to open it.
See also: tmpfile, mkstemp, P_tmpdir.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Once a file has been opened its status can be acquired. As an example
the feof functions determines if the end of the file has been
reached. This can be very useful when reading small parts of a file
at a time. The following example shows how to read one line at a time
from a file until the end has been reached.
filename = "myfile.txt"; fid = fopen (filename, "r"); while (! feof (fid) ) text_line = fgetl (fid); endwhile fclose (fid); |
Note that in some situations it is more efficient to read the entire contents of a file and then process it, than it is to read it line by line. This has the potential advantage of removing the loop in the above code.
Return 1 if an end-of-file condition has been encountered for a given
file and 0 otherwise. Note that it will only return 1 if the end of the
file has already been encountered, not if the next read operation will
result in an end-of-file condition.
See also: fread, fopen, fclose.
Return 1 if an error condition has been encountered for a given file and 0 otherwise. Note that it will only return 1 if an error has already been encountered, not if the next operation will result in an error condition.
Print a list of which files have been opened, and whether they are open for reading, writing, or both. For example,
freport ()
-| number mode name
-|
-| 0 r stdin
-| 1 w stdout
-| 2 w stderr
-| 3 r myfile
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Three functions are available for setting and determining the position of the file pointer for a given file.
Return the position of the file pointer as the number of characters
from the beginning of the file fid.
See also: fseek, fopen, fclose.
Set the file pointer to any location within the file fid.
The pointer is positioned offset characters from the origin,
which may be one of the predefined variables SEEK_CUR (current
position), SEEK_SET (beginning), or SEEK_END (end of
file) or strings "cof", "bof" or "eof". If origin is omitted,
SEEK_SET is assumed. The offset must be zero, or a value returned
by ftell (in which case origin must be SEEK_SET).
Return 0 on success and -1 on error.
See also: ftell, fopen, fclose.
Return the value required to request that fseek perform
one of the following actions:
SEEK_SETPosition file relative to the beginning.
SEEK_CURPosition file relative to the current position.
SEEK_ENDPosition file relative to the end.
Move the file pointer to the beginning of the file fid, returning
0 for success, and -1 if an error was encountered. It is equivalent to
fseek (fid, 0, SEEK_SET).
The following example stores the current file position in the variable
marker, moves the pointer to the beginning of the file, reads
four characters, and then returns to the original position.
marker = ftell (myfile); frewind (myfile); fourch = fgets (myfile, 4); fseek (myfile, marker, SEEK_SET); |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated on December, 26 2007 using texi2html 1.76.