essai6.mod 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. MODULE essai6;
  2. (* test minimal de lecture de fichier char par char*)
  3. FROM IOResult IMPORT ReadResults, ReadResult;
  4. FROM TextIO IMPORT ReadRestLine, SkipLine, ReadToken, WriteLn, WriteString, ReadChar;
  5. FROM SeqFile IMPORT OpenRead, OpenWrite, OpenResults, Close, ChanId, read, write;
  6. IMPORT STextIO, SWholeIO;
  7. VAR
  8. leChanId : ChanId;
  9. leResultat : OpenResults;
  10. unChar : CHAR;
  11. numeroLigne : CARDINAL;
  12. BEGIN
  13. (* principe : on ouvre, on lit les chars les uns après les autres et on les affiche*)
  14. (* une fois arrivé au bout du fichier, on le ferme *)
  15. STextIO.WriteString("Hello you all ! ... be ready for the ISO libs tests !");
  16. STextIO.WriteLn;
  17. STextIO.WriteString("trying to open a file and display the content,char by char");
  18. STextIO.WriteLn;
  19. STextIO.WriteLn;
  20. (* initialisation des variables*)
  21. numeroLigne := 1;
  22. (* ouverture du fichier*)
  23. OpenRead(leChanId, "essai3.mod", read, leResultat);
  24. (* test du résultat*)
  25. IF leResultat=opened THEN
  26. (*le fichier est ouvert*)
  27. STextIO.WriteString("fichier ouvert!");
  28. STextIO.WriteLn;
  29. STextIO.WriteLn;
  30. (* on lit un char*)
  31. ReadChar(leChanId, unChar);
  32. (* on teste le résulat de lecture du char*)
  33. (* si c'est bon, on continue *)
  34. WHILE ReadResult(leChanId) <> endOfInput DO
  35. (* on teste si on est en fin de ligne*)
  36. IF ReadResult(leChanId) = endOfLine THEN
  37. INC(numeroLigne);
  38. (* on est obligé d'utiliser cette fonction poue "passer" la fin de ligne*)
  39. (* sinon, boucle infinie .... car on n'arrive jamais à enOfInput*)
  40. SkipLine(leChanId);
  41. END;
  42. (* on affiche le char*)
  43. STextIO.WriteChar(unChar);
  44. (* on lit le char suivant*)
  45. ReadChar(leChanId, unChar);
  46. END;
  47. ELSE
  48. STextIO.WriteString("Error");
  49. STextIO.WriteLn;
  50. END;
  51. Close(leChanId);
  52. STextIO.WriteLn;
  53. STextIO.WriteLn;
  54. STextIO.WriteString(" nombre de lignes : ");
  55. SWholeIO.WriteCard(numeroLigne, 6);
  56. STextIO.WriteLn;
  57. END essai6.