gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Losing character between SkipLine and ReadString from TextIO


From: Rudolf Schubert
Subject: Losing character between SkipLine and ReadString from TextIO
Date: Sun, 7 May 2023 20:50:47 +0200 (CEST)

Hi,

I recently hit problems with my 'big squash program' when using SkipLine and
ReadString from TextIO when reading text from files. 

In the meantime I've done some first steps with 'cgdb' and could indeed see
that there is a problem somewhere in the 'lower IO routines'. My knowledge
does not suffice to point out exactly what's going wrong. Perhaps it has
something to do with a missing doUnReadChar when 'switching' from SkipLine
to ReadString? But I'm just guessing.

My question would just be: could someone please have a look at my test program
which I've included here? I'm creating a file with 2 lines of text in it.
Then after having created the file I'm opening it again, calling SkipLine
in order to jump to the second line. A ReadString should then read the second
line but obviously the first character of the second line is missing from the
output then.


Here is my test program:

(BTW is it ok to put the program here as text or should I better append it?)

MODULE port_test6_gm2;

FROM ChanConsts IMPORT
  OpenResults, old, read, write;
FROM IOChan IMPORT
  ChanId;
FROM StdChans IMPORT
  StdOutChan;
IMPORT StreamFile;
FROM TextIO IMPORT
  ReadString, SkipLine, WriteLn, WriteString;


PROCEDURE SkipReadTest();
  CONST
    arr_len=                    128;

  TYPE
    arr_type=                   ARRAY[0..arr_len-1] OF CHAR;

  VAR
    cid_file:                   ChanId;
    cid_out:                    ChanId;
    file_name:                  arr_type;
    a_str:                      arr_type;
    res:                        OpenResults;

  BEGIN                         (* PROCEDURE SkipReadTest *)
    cid_out:=StdOutChan();

    file_name:='test_data';

    (* create file and write 2 lines to it *)
    StreamFile.Open(cid_file, file_name, write+old, res);
    IF res=opened THEN
      WriteString(cid_file, '# line1');
      WriteLn(cid_file);
      WriteString(cid_file, '# line2');
      WriteLn(cid_file);
      StreamFile.Close(cid_file);
    END;                        (* IF res=opened *)

    (* (re-)open file and read from it *)
    StreamFile.Open(cid_file, file_name, read, res);
    IF res=opened THEN
      SkipLine(cid_file);
      ReadString(cid_file, a_str);
      WriteString(cid_out, a_str);
      WriteLn(cid_out);
      StreamFile.Close(cid_file);
    END;                        (* IF res=opened *)
  END SkipReadTest;


BEGIN                           (* MODULE port_test6_gm2 *)
  SkipReadTest();
END port_test6_gm2.


BR

Rudolf

--
Rudolf Schubert                 \
Kirchstr. 18a                    \  mailto:rudolf@muc.de
82054 Sauerlach                   > http://www.dose.muc.de
Deutschland                      /
Tel. 08104/908311               /




reply via email to

[Prev in Thread] Current Thread [Next in Thread]