Page 1 sur 1
[R] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 12:11
par pierrotm777
Dans un script , j'ai une fonction qui test la présence de trois fichiers différents .
Seulement, si le fichier choisi n'éxiste pas , autoit me retourne une erreur que je ne sais pas gérer.
'Array variable subscript badly formatted.'
► Afficher le texte
Func _getMot($difficulte)
Local $file = 0
Local $nbr_mots
If $difficulte = 1 Then
$file = FileOpen("dico1", 0)
$nbr_mots = _FileCountLines("dico1")
ElseIf $difficulte = 2 Then
$file = FileOpen("dico2", 0)
$nbr_mots = _FileCountLines("dico2")
ElseIf $difficulte = 3 Then
$file = FileOpen("dico3", 0)
$nbr_mots = _FileCountLines("dico3")
EndIf
local $pos_mot = Random(1, $nbr_mots, 1)
local $mot = FileReadLine($file, $pos_mot)
FileClose($file)
return $mot
EndFunc
Merci de votre aide
Re: [..] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 12:18
par zeshrek
Tu n'as pas mis de gestion d'erreur a l'ouverture de tes fichiers
Là ca devrait aller mieux :
► Afficher le texte
Code : Tout sélectionner
Func _getMot($difficulte)
Local $file = 0
Local $nbr_mots
If $difficulte = 1 Then
$file = FileOpen("dico1", 0)
If $file = -1 Then
MsgBox(0, "Erreur", "Fichier inexistant ou illisible")
Exit
EndIf
$nbr_mots = _FileCountLines("dico1")
ElseIf $difficulte = 2 Then
$file = FileOpen("dico2", 0)
If $file = -1 Then
MsgBox(0, "Erreur", "Fichier inexistant ou illisible")
Exit
EndIf
$nbr_mots = _FileCountLines("dico2")
ElseIf $difficulte = 3 Then
$file = FileOpen("dico3", 0)
If $file = -1 Then
MsgBox(0, "Erreur", "Fichier inexistant ou illisible")
Exit
EndIf
$nbr_mots = _FileCountLines("dico3")
EndIf
Local $pos_mot = Random(1, $nbr_mots, 1)
Local $mot = FileReadLine($file, $pos_mot)
FileClose($file)
Return $mot
EndFunc ;==>_getMot
Re: [..] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 12:18
par Yogui
Bonjour,
Vous n'avez mis que la fonction je ne peux donc pas savoir si vous tester la présence des fichiers.
Si vous ne la testez pas commencer par une condition avec fileexists()
Code : Tout sélectionner
If FileExists("C:\autoexec.bat") Then
MsgBox(4096, "C:\autoexec.bat File", "Exists") ; votre fonction
Else
MsgBox(4096,"C:\autoexec.bat File", "Does NOT exists") ; un message indiquant que le fichier n'est pas présent
EndIf
Re: [..] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 12:42
par pierrotm777
C'est parfait, merci encore à tous les deux
Re: [R] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 16:23
par bloodwolff
En un peu plus court si tu veux
► Afficher le texte
Code : Tout sélectionner
Func _getMot($difficulte)
Local $file = 0
Local $nbr_mots
$nom = "dico" & $difficulte ;~ & ".ext"
$file = FileOpen($nom)
If $file = -1 Then
MsgBox(0, "Erreur", "Fichier inexistant ou illisible")
;~ SetError(1) pour gérer les erreurs si tu veux
Return ;~ Return plus approprié qu'un exit je pense
EndIf
$nbr_mots = _FileCountLines($nom)
Local $pos_mot = Random(1, $nbr_mots, 1)
Local $mot = FileReadLine($file, $pos_mot)
FileClose($file)
Return $mot
EndFunc ;==>_getMot
Re: [R] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 17:33
par pierrotm777
J'ai ajouté aprés le message la création d'un dossier puis du fichier ainsi:
► Afficher le texte
Code : Tout sélectionner
DirCreate(@ScriptDir &"\language\"&$language)
_FileCreate(@ScriptDir &"\language\"&$language&"\dico1")
Effectivement, return me convient mieux !
Re: [R] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 19:13
par alex1205
Regarde :
Code : Tout sélectionner
Func _getMot($difficulte)
Local $file = 0
Local $nbr_mots
$nom = "dico" & $difficulte ;~ & ".ext"
$file = FileOpen($nom)
If $file = -1 Then
MsgBox(0, "Erreur", "Fichier inexistant ou illisible")
;~ SetError(1) pour gérer les erreurs si tu veux
Return ;~ Return plus approprié qu'un exit je pense
EndIf
$nbr_mots = _FileCountLines($nom)
Local $pos_mot = Random(1, $nbr_mots[color=#FF0000][0][/color], 1)
Local $mot = FileReadLine($file, $pos_mot)
FileClose($file)
Return $mot
EndFunc ;==>_getMot
Il faut mettre [0] pour l'array !
Re: [R] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 19:28
par pierrotm777
Pourquoi faut-il ajouter [0] ?
La fonction semble fonctionner sans !
Re: [R] Ajouter un test d'erreur
Posté : mar. 23 nov. 2010 22:50
par bloodwolff
Tu te goure avec ton [0] !
Va lire l'aide il n'en a nul besoin
Sinon voilà encore plus court et qui utilise le même udf
Code : Tout sélectionner
Func _getMot($difficulte)
Dim $nbr_mots
If Not _FileReadToArray("dico" & $difficulte ,$nbr_mots) Then
MsgBox(0, "Erreur", "Fichier inexistant ou illisible")
;~ SetError(1) pour gérer les erreurs si tu veux
Return ;~ Return plus approprié qu'un exit je pense
EndIf
Return $nbr_mots[Random(1, $nbr_mots[0], 1)]
EndFunc ;==>_getMot