Octave – Input e output – VII – 64

nm3

Oggi, proseguendo da qui, concludo il capitolo, copiando qui.

I/O binario
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.

Built-in Function: val = fread (fid)
Built-in Function: val = fread (fid, size)
Built-in Function: val = fread (fid, size, precision)
Built-in Function: val = fread (fid, size, precision, skip)
Built-in Function: val = fread (fid, size, precision, skip, arch)
Built-in Function: [val, count] = fread (...)

Read binary data from the file specified by the file descriptor fid.
The optional argument size specifies the amount of data to read and may be one of
Qui c’è una tabella che definire panicante è riduttivo. Non la riporto; è qui.

Built-in Function: fwrite (fid, data)
Built-in Function: fwrite (fid, data, precision)
Built-in Function: fwrite (fid, data, precision, skip)
Built-in Function: fwrite (fid, data, precision, skip, arch)
Built-in Function: count = fwrite (...)

Write data in binary form to the file specified by the file descriptor fid, returning the number of values count 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.

o285

Passo qui.

Files temporanei
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.

Built-in Function: [fid, name, msg] = mkstemp ("template")
Built-in Function: [fid, name, msg] = mkstemp ("template", delete)

Return the file descriptor fid 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 in binary mode and with the O_EXCL flag.
If the optional argument delete is supplied and is true, the file will be deleted automatically when Octave exits.
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.

Built-in Function: [fid, msg] = tmpfile ()
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 and 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.

Built-in Function: fname = tempname ()
Built-in Function: fname = tempname (dir)
Built-in Function: fname = tempname (dir, prefix)

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 (P_tmpdir) is used. If dir is provided, it must exist, otherwise the default directory for temporary files is used.
Programming Note: Because the named file is not opened by tempname, it is possible, though relatively unlikely, that it will not be available by the time your program attempts to open it. If this is a concern, see tmpfile.

Function File: dir = tempdir ()
Return the name of the host system’s directory for temporary files.
The directory name is taken first from the environment variable TMPDIR. If that does not exist the system default returned by P_tmpdir is used.

Built-in Function: P_tmpdir ()
Return the name of the host system’s default directory for temporary files.
Programming Note: The value returned by P_tmpdir is always the default location. This value may not agree with that returned from tempdir if the user has overridden the default with the TMPDIR environment variable.

Avanti, qui.

End of file ed errori
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.

o286.png

notare l’errore: fid cessa di esistere alla chiusura del file.

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.

Built-in Function: status = feof (fid)
Return 1 if an end-of-file condition has been encountered for the file specified by file descriptor fid and 0 otherwise.
Note that feof 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.

Built-in Function: msg = ferror (fid)
Built-in Function: [msg, err] = ferror (fid)
Built-in Function: [dots] = ferror (fid, "clear")

Query the error status of the stream specified by file descriptor fid.
If an error condition exists then return a string msg describing the error. Otherwise, return an empty string “”.
The second input "clear" is optional. If supplied, the error state on the stream will be cleared.
The optional second output is a numeric indication of the error status. err is 1 if an error condition has been encountered and 0 otherwise.
Note that ferror indicates if an error has already occurred, not whether the next operation will result in an error condition.

Built-in Function: fclear (fid)
Clear the stream state for the file specified by the file descriptor fid.

Built-in Function: freport ()
Print a list of which files have been opened, and whether they are open for reading, writing, or both.

o287

Avanti ancora, qui.

Posizione nel file
Three functions are available for setting and determining the position of the file pointer for a given file.

Built-in Function: pos = ftell (fid)
Return the position of the file pointer as the number of characters from the beginning of the file specified by file descriptor fid.

Built-in Function: fseek (fid, offset)
Built-in Function: fseek (fid, offset, origin)
Built-in Function: status = fseek (...)

Set the file pointer to the location offset 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. offset may be positive, negative, or zero but not all combinations of origin and offset can be realized.

Built-in Function: SEEK_SET ()
Built-in Function: SEEK_CUR ()
Built-in Function: SEEK_END ()

Return the numerical value to pass to fseek to perform one of the following actions:

  • SEEK_SET Position file relative to the beginning.
  • SEEK_CUR Position file relative to the current position.
  • SEEK_END Position file relative to the end.

Built-in Function: frewind (fid)
Built-in Function: status = frewind (fid)
Move the file pointer to the beginning of the file specified by file descriptor fid.
frewind returns 0 for success, and -1 if an error is 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.

o288

:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: