Fil d’Ariane du forum – Vous êtes ici :ForumForums techniques: AS/400Lecture des enregistrements d'un …
Vous devez vous identifier pour créer des messages et des sujets.

Lecture des enregistrements d'un fichier dans l'IFS via RPGLE

Bonjour,
petite astuces pour déterminer la taille de chaque ligne: toutes les lignes se terminent par un LF
sauf la dernière ligne (donc le %len() ne marche pas à chaque ligne)
=> j'utilise un %scan  LF  à chaque ligne sauf pour la dernière ligne (EndOfFile)  ou j'utilise un %len
--------------------------------

H DFTACTGRP(*NO)
H BNDDIR('QC2LE')

* open()
D fopen PR * ExtProc('_C_IFS_fopen')
D fildes * value options(*string)
D mode * value options(*string)
* fgets()
D fgets PR * ExtProc('_C_IFS_fgets')
D buf * value
D size 10I 0 value
D fildes * value
* close()
D close PR 10I 0 ExtProc('_C_IFS_fclose')
D fildes * value
*
* -
* __errno
D get_errno PR * ExtProc('__errno')
D ptrToErrno S *
D errno S 10I 0 based(ptrToErrno)
*
* variables ---
D fd S *
D nomfic S 32A
D idmsg S 7A
D ret S 10I 0
D data S 1000A
D ligne S 1000A VARYING
D w50a S 50A VARYING
D wtaille S 4S 0
D LF S 2A Inz(x'25')
D LFPOS S 10I 0 Inz(0)
D CRLF S 2A Inz(x'0D25')
D CRLFPos S 10I 0 Inz(0)
D targetVar s 50a Varying
*
* astuce  ( pour convertir un UTF8  1208   en  ANSI  1252  )
* CPY OBJ('/home/TVO/a.txt') TOOBJ(/home/TVO/a2.txt) FROMCCSID(1208) TOCCSID(1252) DTAFMT(*TEXT)
* debut programme en free
/free
// initialisation
nomfic = '/home/tvo/a2.txt' ;
// .
// Ouverture du fichier
fd = fopen(%trim(nomfic): 'r') ;
if fd = *null ;
// erreur ouverture
ptrToErrno = get_errno();
idmsg = 'CPE' + %char(errno);
dsply idmsg;
*inlr = *on;
return;
endif ;
// .
// BOUCLE de lecture du fichier dans l IFS
dow (fgets(%addr(data): %size(data): fd) <> *null);
ligne = %str(%addr(data)) ;
// recherche du LF ( line feed )
targetVar = %Trim(LF) ;
LFpos = %scan(targetvar : ligne) ;
if LFpos > 0 ;
// LF trouvé
w50a = %Subst(ligne:01:LFpos-1) ;
else ;
// LF non trouvé, alors on est sur la dernière ligne du fichier
wtaille = %len(ligne);
w50a = %Subst(ligne:01:wtaille) ;
endif ;
// dsply
dsply w50a;
//
enddo;
// .
// fermeture fichier
ret = close(fd);
// .
// FIN PGM
*INLR = '1';
RETURN;
/end-free