Perror ()

Da Gambas-it.org - Wikipedia.

La funzione

void perror (const char *__s)

scrive nello standard error un messaggio descrittivo di un errore.

La funzione esterna perror( ) andrà utilizzata in Gambas solo per mostrare eventuali errori derivanti da funzioni esterne, dichiarate con Extern.

Volendo utilizzare tale funzione in Gambas, bisognerà dichiararla con Extern, nonché bisognerà dichiarare la libreria di C: libc.so.6, nella quale essa è contenuta:

Private Extern perror(__s As String)


Mostriamo un semplice esempio, nel quale si tenterà di aprire un file inesistente all'interno della cartella /tmp:

Library "libc:6"

' FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)
' Open a file and create a new stream for it.
Private Extern fopen(__filename As String, __modes As String) As Pointer

' int perror (const char *__s)
' Print a message describing the meaning of the value of errno.
Private Extern perror(__s As String)

' int fclose (FILE *__stream)
' Close STREAM.
Private Extern fclose(__stream As Pointer) As Integer


Public Sub Main()
 
 Dim p As Pointer
 
' Questa è la funzione esterna di cui si mostrerà con "perror()" l'errore sollevato:
  p = fopen("/tmp/xxxxxx", "r")
  If p = 0 Then perror("Errore: ")
  
  fclose(p)
  
End



Riferimenti