Differenze tra le versioni di "Sscanf ()"

Da Gambas-it.org - Wikipedia.
(Creata pagina con "La funzione della libreria di C ''int sscanf (const char *__restrict __s, const char *__restrict __format, ...)'' legge l'input formattato da una stringa. <BR>Questa funzione...")
 
Riga 14: Riga 14:
 
  <FONT Color=gray>' ''int sscanf (const char *__restrict __s, const char *__restrict __format, ...)''
 
  <FONT Color=gray>' ''int sscanf (const char *__restrict __s, const char *__restrict __format, ...)''
 
  ' ''Read formatted input from S.''</font>
 
  ' ''Read formatted input from S.''</font>
  Private Extern sscanf(__s As Pointer, __format As String, po2 As Pointer, po3 As Pointer, po4 As Pointer, po5 As Pointer) As Integer
+
  Private Extern sscanf(__s As String, __format As String, po2 As Pointer, po3 As Pointer, po4 As Pointer, po5 As Pointer) As Integer
 
   
 
   
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
    
 
    
   Dim p1, p2, p3 As Pointer
+
  Dim s As String
 +
   Dim p1, p2 As Pointer
 
   Dim i As Integer
 
   Dim i As Integer
 
   Dim f As Float
 
   Dim f As Float
 
    
 
    
   p1 = Alloc("aaaa bbbb 11111 123,4567")
+
   s = "aaaa bbbb 11111 123,4567"
 +
  p1 = Alloc(SizeOf(gb.Byte), 16)
 
   p2 = Alloc(SizeOf(gb.Byte), 16)
 
   p2 = Alloc(SizeOf(gb.Byte), 16)
  p3 = Alloc(SizeOf(gb.Byte), 16)
 
 
    
 
    
   <FONT color=#B22222>sscanf</font>(p1, "%s %s %d  %lf", p2, p3, VarPtr(i), VarPtr(f))
+
   <FONT color=#B22222>sscanf</font>(s, "%s %s %d  %lf", p1, p2, VarPtr(i), VarPtr(f))
 
    
 
    
   Print String@(p2), String@(p3), i, f
+
   Print String@(p1), String@(p2), i, f
 
    
 
    
  Free(p3)
 
 
   Free(p2)
 
   Free(p2)
 
   Free(p1)
 
   Free(p1)
Riga 42: Riga 42:
 
=Riferimenti=
 
=Riferimenti=
 
* https://linux.die.net/man/3/sscanf
 
* https://linux.die.net/man/3/sscanf
 +
* http://www.gnu.org/software/libc/manual/html_node/Formatted-Input-Functions.html

Versione delle 19:29, 11 feb 2017

La funzione della libreria di C

int sscanf (const char *__restrict __s, const char *__restrict __format, ...)

legge l'input formattato da una stringa.
Questa funzione pertanto è simile alla funzione esterna scanf( ), ma l'input avviene dal primo parametro __s, ossia da un Puntatore di tipo char.


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

Private Extern sscanf(__s As Pointer, __format As String, [qui altri eventuali parametri As Pointer]) As Integer In "libc:6"


Mostriamo un esempio pratico:

Library "libc:6"

' int sscanf (const char *__restrict __s, const char *__restrict __format, ...)
' Read formatted input from S.
Private Extern sscanf(__s As String, __format As String, po2 As Pointer, po3 As Pointer, po4 As Pointer, po5 As Pointer) As Integer


Public Sub Main()
 
 Dim s As String
 Dim p1, p2 As Pointer
 Dim i As Integer
 Dim f As Float
 
  s = "aaaa bbbb 11111 123,4567"
  p1 = Alloc(SizeOf(gb.Byte), 16)
  p2 = Alloc(SizeOf(gb.Byte), 16)
  
  sscanf(s, "%s %s %d  %lf", p1, p2, VarPtr(i), VarPtr(f))
  
  Print String@(p1), String@(p2), i, f
  
  Free(p2)
  Free(p1)
  
End



Riferimenti