Octave – Input e output – III – 60

mind-evolved

Continuo copiando qui.

Funzioni di I/O come nel C

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.

Built-in Function: stdin ()
Return the numeric value corresponding to the standard input stream.
When Octave is used interactively, stdin is filtered through the command line editing functions.

Built-in Function: stdout ()
Return the numeric value corresponding to the standard output stream.
Data written to the standard output is normally filtered through the pager.

Built-in Function: 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.

Avanti, qui.

Aprire e chiudere files
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... (write in this case)
fclose (fid);

Built-in Function: fid = fopen (name)
Built-in Function: fid = fopen (name, mode)
Built-in Function: fid = fopen (name, mode, arch)
Built-in Function: [fid, msg] = fopen (...)
Built-in Function: fid_list = fopen ("all")
Built-in Function: [file, mode, arch] = fopen (fid)

Open a file for low-level I/O or query open files and file descriptors.
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

  • r’ (default) Open a file for reading.
  • w’ Open a file for writing. The previous contents are discarded.
  • a’ Open or create a file for writing at the end of the file.
  • r+’ Open an existing file for reading and writing.
  • w+’ Open a file for reading or writing. The previous contents are discarded.
  • a+’ 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 when 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" or "n" (default) The format of the current machine.
  • "ieee-be" or "b" IEEE big endian format.
  • "ieee-le" or "l" IEEE little endian format.

However, conversions are currently only supported for ‘native’, ‘ieee-be’, and ‘ieee-le’ formats.
When opening a new file that does not yet exist, permissions will be set to 0666 – umask.

Built-in Function: fclose (fid)
Built-in Function: fclose ("all")
Built-in Function: status = fclose ("all")

Close the file specified by the file descriptor fid.
If successful, fclose returns 0, otherwise, it returns -1. The second form of the fclose call closes all open files except stdout, stderr, and stdin.
Programming Note: When using "all" the file descriptors associated with gnuplot will also be closed. This will prevent further plotting with gnuplot until Octave is closed and restarted.

Function File: is_valid_file_id (fid)
Return true if fid refers to an open file.

Avanti, qui.

Output elementare
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’.

o270

Nota: meglio aggiungere “\n” per l’a-capo:

o271

Built-in Function: fputs (fid, string)
Built-in Function: status = fputs (fid, string)

Write the string string to the file with file descriptor fid.
The string is written to the file with no additional formatting. Use fdisp instead to automatically append a newline character appropriate for the local machine.
Return a non-negative number on success or 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.

Built-in Function: puts (string)
Built-in Function: status = puts (string)

Write a string to the standard output with no formatting.
The string is written verbatim to the standard output. Use disp to automatically append a newline character appropriate for the local machine.
Return a non-negative number on success and EOF on error.

Avanti ancora, qui.

Input per righe
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

o272

Built-in Function: str = fgetl (fid)
Built-in Function: str = fgetl (fid, len)

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.
To read a line and return the terminating newline see fgets.

Built-in Function: str = fgets (fid)
Built-in Function: str = fgets (fid, len)

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.
To read a line and discard the terminating newline see fgetl.

o273

Built-in Function: nlines = fskipl (fid)
Built-in Function: nlines = fskipl (fid, count)
Built-in Function: nlines = fskipl (fid, Inf)

Read and skip count lines from the file specified by the file descriptor fid.
fskipl discards characters until an end-of-line is encountered exactly count-times, or until the end-of-file marker is found.
If count is omitted, it defaults to 1. count may also be Inf, in which case lines are skipped until the end of the file. This form is suitable for counting the number of lines in a file.
Returns the number of lines skipped (end-of-line sequences encountered).

o274

Pausa, sarà un capitolo lungo 😳
:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

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: