How to resolve the algorithm Read a file character by character/UTF8 step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Read a file character by character/UTF8 step by step in the REXX programming language

Table of Contents

Problem Statement

Read a file one character at a time, as opposed to reading the entire file at once. The solution may be implemented as a procedure, which returns the next character in the file on each consecutive call (returning EOF when the end of the file is reached). The procedure should support the reading of files containing UTF8 encoded wide characters, returning whole characters for each consecutive read.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Read a file character by character/UTF8 step by step in the REXX programming language

Source code in the rexx programming language

/*REXX program  reads and displays  a file char by char, returning   'EOF'   when done. */
parse arg iFID .                                 /*iFID:     is the fileID to be read.  */
                                                 /* [↓]  show the file's contents.      */
if iFID\==''  then do j=1  until  x=='EOF'       /*J  count's the file's characters.    */
                   x=getchar(iFID);    y=        /*get a character  or  an 'EOF'.       */
                   if x>>' '   then y=x          /*display   X   if presentable.        */
                   say  right(j, 12)     'character,  (hex,char)'      c2x(x)      y
                   end   /*j*/                   /* [↑]  only display  X  if not low hex*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
getchar: procedure;  parse arg z;  if chars(z)==0  then return 'EOF';     return charin(z)


/* REXX ---------------------------------------------------------------
* 29.12.2013 Walter Pachl
* read one utf8 character at a time
* see http://de.wikipedia.org/wiki/UTF-8#Kodierung
*--------------------------------------------------------------------*/
oid='utf8.txt';'erase' oid /* first create file containing utf8 chars*/
Call charout oid,'79'x
Call charout oid,'C3A4'x
Call charout oid,'C2AE'x
Call charout oid,'E282AC'x
Call charout oid,'F09D849E'x
Call lineout oid
fid='utf8.txt'             /* then read it and show the contents     */
Do Until c8='EOF'
  c8=get_utf8char(fid)
  Say left(c8,4) c2x(c8)
  End
Exit

get_utf8char: Procedure
  Parse Arg f
  If chars(f)=0 Then
    Return 'EOF'
  c=charin(f)
  b=c2b(c)
  If left(b,1)=0 Then
    Nop
  Else Do
    p=pos('0',b)
    Do i=1 To p-2
      If chars(f)=0 Then Do
        Say 'illegal contents in file' f
        Leave
        End
      c=c||charin(f)
      End
    End
  Return c

c2b: Return x2b(c2x(arg(1)))


  

You may also check:How to resolve the algorithm Integer overflow step by step in the Perl programming language
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Prime triangle step by step in the BASIC programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Elixir programming language