lecture de fichier avec ReadString()
Publié : mar. 10/oct./2006 16:43
Bonjour ,
Etant debutant en purebasic j'ai une question à poser :
Lorsque j'essaye de lire un fichier txt avec ReadString() , la lecture s'arrette au premier retour à la ligne dans le fichier .
J'aimerai savoir comment contourner cela en pouvant lire de maniere continue tout ce qui est contenu dans mon fichier sans être preoccupper par la mise en forme du dit fichier txt ( retour à la ligne ou pas , espace vide important etc etc ) .
Je bloque en effet là dessus .
Merci pour votre aide precieuse .
jnoel
Voici mon code ( non finalisé ) :
; PROGRAMME DECODAGE D'UN TEXTE CODE EN ASCII
;ON DECLARE LES VARIABLES
Global L
Global text$
Global code_ascii$
Global x
Global caractere$
Global C$
Global M
M=0
altgr=0
x=0
curseur=0
y=0
retour=0
R=0
r=0
t=0
z=0
c=0
;ON DECLARE LES TABLEAUX
Dim C$(100000)
Dim C(100000)
;ON OUVRE LA CONSOLE ET LE FICHIER TEXTE
OpenConsole()
OpenFile(0, "fichier.txt")
;ICI ON LIT LE FICHIER TEXTE
text$ = ReadString() ; Ici la lecture s'arrette au premier retour à la ligne dans le fichier , comment remedier à cela ?
L=(Len(text$))+1
Print(Str(L))
;PrintN("text$="+text$)
;Delay(1000)
;Print(Str(L))
;ICI ON INITIALISE A VIDE LE TABLEAU QUI CONTIENDRA CHAQUE CODE DE CARACTERE
For x=0 To 100000
C$(x)=("")
Next
;ICI ON VA PLACER LES CODES ASCII DE KEY.TX DANS LE TABLEAU
x=0
t=0
Repeat
caractere$= Mid(text$,(x+1),1)
;Print(caractere$) ;caractere represente le caractere en cours de lecture contenue dans le fichier key.txt
;Delay(1000)
If caractere$=" " Or caractere$="" ;si on rencontre un espace vide "" ou " " cela indique la fin du premier code ascci alors on l'affecte dans le tableau
z=z+1;la variable z sert a trouver le dernier code ascii dans le tableau si z>1
t=t+1; la variable t represente le nombre de code sans les espaces vide dans le fichier key.txt
;PrintN(code_ascii$+" t="+Str(t)+" x="+Str(x)+" z="+Str(z))
;Delay(1000)
C$(t)=code_ascii$ ;ICI ON PLACE LE CODE ASCII DANS LE TABLEAU
C=Val(C$(t))
code_ascii$="" ;puis on vide la variable code_ascii pour passer au code ascii suivant
Else
z=0
code_ascii$=code_ascii$+caractere$;ici on recree le code ascii en additionant les caracteres qui le compose
EndIf
;Delay(100)
;PrintN(code_ascii$+" t="+Str(t)+" x="+Str(x)+" z="+Str(z))
x=x+1
Until z>10 ; boucle tant que l'on a pas affecter tout les codes ASCII dans le tableau on recommence
;MAINTENANT QUE L'ON A AFFECTE CHAQUE CODE ASCII DANS LE TABLEAU ON VA DECODER ET AFFICHER CHAQUE CODE ASCII DECODE
;ON COMMENCE PAR EXTRAIRE LE CODE ASCII DU TABLEAU
h=t-1
For x=1 To h;ATTENTION X ne doit pas depasser la valeur definis dans le tableau DIM C$()
C$=C$(x)
C=Val(C$)
C(x)=C
;ICI ON VERIFIE SI ON EST EN MODE MAJUSCULE OU MINUSCULE
If C=20 And M=0
M=1 ;on passe en mode majuscule
Else
If C=20 And M=1
M=0 ;on passe en mode minuscule
EndIf
EndIf
;ICI ON VERIFIE SI ALTGR A ETE PRESSE
If C=18
altgr=1
EndIf
;ICI ON VA TRAITER LES CODES ASCII POUR LES AFFICHER EN MODE TEXTE
;POUR COMMENCER ON NE VAS PAS AFFICHER LES CARACTERES DE CONTROLE TYPE TOUCHE ENTREE
;PrintN(" ")
;PrintN(" M="+Str(M)+" C="+C$)
;Delay(1000)
If Mid(C$,1,1)="D" Or Mid(C$,1,1)="H"
PrintN(" ")
Print(C$+" ")
C$=" "
EndIf
Delay(20)
Select M
;MODE MAJUSCULE SI M=1
Case 1
Select C$
;CLAVIER NUMERIQUE
Case "96" ; chiffre 0
Print("0")
Case "97" ; chiffre 1
Print("1")
Case "98" ; chiffre 2
Print("2")
Case "99" ; chiffre 3
Print("3")
Case "100" ; chiffre 4
Print("4")
Case "101" ; chiffre 5
Print("5")
Case "102" ; chiffre 6
Print("6")
Case "103" ; chiffre 7
Print("7")
Case "104" ; chiffre 8
Print("8")
Case "105" ; chiffre 9
Print("9")
Case "110" ; si le code ASCII est un point on l'affiche
Print(".")
;AUTRES CARACTERES
Case "32" ; si le code ASCII est un espace on l'affiche
Print(" ")
Case "13" ; si le code ASCII est 13 (touche entree) on affiche un retour a la ligne
PrintN("")
;pour les caracteres virgule ,point virgule , deux points, point d'exclamation
Case "-68"
Print("?")
Case "-66"
Print(".")
Case "-65"
Print("/")
Case "-33"
Print("§")
;pour les touches 0 a 9 du haut du clavier
Case "48"
If altgr=0 ; si altgr=0 on affiche "0"
Print("0")
Else
Print("@") ; si altgr =1 on affiche le caractere arobase
altgr=0 ; on remet altgr a zero
EndIf
Case "49"
Print("1")
Case "50"
Print("2")
Case "51"
Print("3")
Case "52"
Print("4")
Case "53"
Print("5")
Case "54"
Print("6")
Case "55"
Print("7")
Case "56"
Print("8")
Case "57"
Print("9")
Case "-69"
Print("+")
Default
;TOUCHES A a Z
If (C>64) And (C<91)
C=Val(C$)
C=C
C$=Chr(C)
Print(C$)
EndIf
EndSelect
;MODE MINUSCULE SI M=0
Case 0
Select C$
;CLAVIER NUMERIQUE
Case "96" ; chiffre 0
Print("0")
Case "97" ; chiffre 1
Print("1")
Case "98" ; chiffre 2
Print("2")
Case "99" ; chiffre 3
Print("3")
Case "100" ; chiffre 4
Print("4")
Case "101" ; chiffre 5
Print("5")
Case "102" ; chiffre 6
Print("6")
Case "103" ; chiffre 7
Print("7")
Case "104" ; chiffre 8
Print("8")
Case "105" ; chiffre 9
Print("9")
Case "110" ; si le code ASCII est un point on l'affiche
Print(".")
;AUTRES CARACTERES
Case "d"
Print("d")
Case "32" ; si le code ASCII est un espace on l'affiche
Print(" ")
Case "13" ; si le code ASCII est 13 (touche entree) on affiche un retour a la ligne
PrintN("")
;pour les caracteres virgule ,point virgule , deux points, point d'exclamation
Case "188"
Print(",")
Case "190"
Print(";")
Case "191"
Print(":")
Case "223"
Print("!")
;pour les touches 0 a 9 du haut du clavier
Case "48"
If altgr=0 ; si altgr=0 on affiche "à"
Print("a")
Else
Print("@") ; si altgr =1 on affiche le caractere arobase
altgr=0 ; on remet altgr a zero
EndIf
Case "49"
Print("&")
Case "50"
Print("e")
Case "51"
Print("''")
Case "52"
Print("'")
Case "53"
Print("(")
Case "54"
Print("-")
Case "55"
Print("e")
Case "56"
Print("_")
Case "57"
Print("c")
Case "-69"
Print("=")
Case "-37"
Print(")")
Default
;TOUCHES A a Z
If (C>64) And (C<91)
C=Val(C$)
C=C+32
C$=Chr(C)
Print(C$)
EndIf
EndSelect
EndSelect
Next
Input()
Etant debutant en purebasic j'ai une question à poser :
Lorsque j'essaye de lire un fichier txt avec ReadString() , la lecture s'arrette au premier retour à la ligne dans le fichier .
J'aimerai savoir comment contourner cela en pouvant lire de maniere continue tout ce qui est contenu dans mon fichier sans être preoccupper par la mise en forme du dit fichier txt ( retour à la ligne ou pas , espace vide important etc etc ) .
Je bloque en effet là dessus .
Merci pour votre aide precieuse .
jnoel
Voici mon code ( non finalisé ) :
; PROGRAMME DECODAGE D'UN TEXTE CODE EN ASCII
;ON DECLARE LES VARIABLES
Global L
Global text$
Global code_ascii$
Global x
Global caractere$
Global C$
Global M
M=0
altgr=0
x=0
curseur=0
y=0
retour=0
R=0
r=0
t=0
z=0
c=0
;ON DECLARE LES TABLEAUX
Dim C$(100000)
Dim C(100000)
;ON OUVRE LA CONSOLE ET LE FICHIER TEXTE
OpenConsole()
OpenFile(0, "fichier.txt")
;ICI ON LIT LE FICHIER TEXTE
text$ = ReadString() ; Ici la lecture s'arrette au premier retour à la ligne dans le fichier , comment remedier à cela ?
L=(Len(text$))+1
Print(Str(L))
;PrintN("text$="+text$)
;Delay(1000)
;Print(Str(L))
;ICI ON INITIALISE A VIDE LE TABLEAU QUI CONTIENDRA CHAQUE CODE DE CARACTERE
For x=0 To 100000
C$(x)=("")
Next
;ICI ON VA PLACER LES CODES ASCII DE KEY.TX DANS LE TABLEAU
x=0
t=0
Repeat
caractere$= Mid(text$,(x+1),1)
;Print(caractere$) ;caractere represente le caractere en cours de lecture contenue dans le fichier key.txt
;Delay(1000)
If caractere$=" " Or caractere$="" ;si on rencontre un espace vide "" ou " " cela indique la fin du premier code ascci alors on l'affecte dans le tableau
z=z+1;la variable z sert a trouver le dernier code ascii dans le tableau si z>1
t=t+1; la variable t represente le nombre de code sans les espaces vide dans le fichier key.txt
;PrintN(code_ascii$+" t="+Str(t)+" x="+Str(x)+" z="+Str(z))
;Delay(1000)
C$(t)=code_ascii$ ;ICI ON PLACE LE CODE ASCII DANS LE TABLEAU
C=Val(C$(t))
code_ascii$="" ;puis on vide la variable code_ascii pour passer au code ascii suivant
Else
z=0
code_ascii$=code_ascii$+caractere$;ici on recree le code ascii en additionant les caracteres qui le compose
EndIf
;Delay(100)
;PrintN(code_ascii$+" t="+Str(t)+" x="+Str(x)+" z="+Str(z))
x=x+1
Until z>10 ; boucle tant que l'on a pas affecter tout les codes ASCII dans le tableau on recommence
;MAINTENANT QUE L'ON A AFFECTE CHAQUE CODE ASCII DANS LE TABLEAU ON VA DECODER ET AFFICHER CHAQUE CODE ASCII DECODE
;ON COMMENCE PAR EXTRAIRE LE CODE ASCII DU TABLEAU
h=t-1
For x=1 To h;ATTENTION X ne doit pas depasser la valeur definis dans le tableau DIM C$()
C$=C$(x)
C=Val(C$)
C(x)=C
;ICI ON VERIFIE SI ON EST EN MODE MAJUSCULE OU MINUSCULE
If C=20 And M=0
M=1 ;on passe en mode majuscule
Else
If C=20 And M=1
M=0 ;on passe en mode minuscule
EndIf
EndIf
;ICI ON VERIFIE SI ALTGR A ETE PRESSE
If C=18
altgr=1
EndIf
;ICI ON VA TRAITER LES CODES ASCII POUR LES AFFICHER EN MODE TEXTE
;POUR COMMENCER ON NE VAS PAS AFFICHER LES CARACTERES DE CONTROLE TYPE TOUCHE ENTREE
;PrintN(" ")
;PrintN(" M="+Str(M)+" C="+C$)
;Delay(1000)
If Mid(C$,1,1)="D" Or Mid(C$,1,1)="H"
PrintN(" ")
Print(C$+" ")
C$=" "
EndIf
Delay(20)
Select M
;MODE MAJUSCULE SI M=1
Case 1
Select C$
;CLAVIER NUMERIQUE
Case "96" ; chiffre 0
Print("0")
Case "97" ; chiffre 1
Print("1")
Case "98" ; chiffre 2
Print("2")
Case "99" ; chiffre 3
Print("3")
Case "100" ; chiffre 4
Print("4")
Case "101" ; chiffre 5
Print("5")
Case "102" ; chiffre 6
Print("6")
Case "103" ; chiffre 7
Print("7")
Case "104" ; chiffre 8
Print("8")
Case "105" ; chiffre 9
Print("9")
Case "110" ; si le code ASCII est un point on l'affiche
Print(".")
;AUTRES CARACTERES
Case "32" ; si le code ASCII est un espace on l'affiche
Print(" ")
Case "13" ; si le code ASCII est 13 (touche entree) on affiche un retour a la ligne
PrintN("")
;pour les caracteres virgule ,point virgule , deux points, point d'exclamation
Case "-68"
Print("?")
Case "-66"
Print(".")
Case "-65"
Print("/")
Case "-33"
Print("§")
;pour les touches 0 a 9 du haut du clavier
Case "48"
If altgr=0 ; si altgr=0 on affiche "0"
Print("0")
Else
Print("@") ; si altgr =1 on affiche le caractere arobase
altgr=0 ; on remet altgr a zero
EndIf
Case "49"
Print("1")
Case "50"
Print("2")
Case "51"
Print("3")
Case "52"
Print("4")
Case "53"
Print("5")
Case "54"
Print("6")
Case "55"
Print("7")
Case "56"
Print("8")
Case "57"
Print("9")
Case "-69"
Print("+")
Default
;TOUCHES A a Z
If (C>64) And (C<91)
C=Val(C$)
C=C
C$=Chr(C)
Print(C$)
EndIf
EndSelect
;MODE MINUSCULE SI M=0
Case 0
Select C$
;CLAVIER NUMERIQUE
Case "96" ; chiffre 0
Print("0")
Case "97" ; chiffre 1
Print("1")
Case "98" ; chiffre 2
Print("2")
Case "99" ; chiffre 3
Print("3")
Case "100" ; chiffre 4
Print("4")
Case "101" ; chiffre 5
Print("5")
Case "102" ; chiffre 6
Print("6")
Case "103" ; chiffre 7
Print("7")
Case "104" ; chiffre 8
Print("8")
Case "105" ; chiffre 9
Print("9")
Case "110" ; si le code ASCII est un point on l'affiche
Print(".")
;AUTRES CARACTERES
Case "d"
Print("d")
Case "32" ; si le code ASCII est un espace on l'affiche
Print(" ")
Case "13" ; si le code ASCII est 13 (touche entree) on affiche un retour a la ligne
PrintN("")
;pour les caracteres virgule ,point virgule , deux points, point d'exclamation
Case "188"
Print(",")
Case "190"
Print(";")
Case "191"
Print(":")
Case "223"
Print("!")
;pour les touches 0 a 9 du haut du clavier
Case "48"
If altgr=0 ; si altgr=0 on affiche "à"
Print("a")
Else
Print("@") ; si altgr =1 on affiche le caractere arobase
altgr=0 ; on remet altgr a zero
EndIf
Case "49"
Print("&")
Case "50"
Print("e")
Case "51"
Print("''")
Case "52"
Print("'")
Case "53"
Print("(")
Case "54"
Print("-")
Case "55"
Print("e")
Case "56"
Print("_")
Case "57"
Print("c")
Case "-69"
Print("=")
Case "-37"
Print(")")
Default
;TOUCHES A a Z
If (C>64) And (C<91)
C=Val(C$)
C=C+32
C$=Chr(C)
Print(C$)
EndIf
EndSelect
EndSelect
Next
Input()