Menu

Writing to sequential file

Help
2002-08-25
2012-12-29
  • George James

    George James - 2002-08-25

    Does anyone know how to create a sequential file without getting a linefeed at the end?

    If I create a file using stream or variable format and write some data to it then I get an unwanted linefeed character at the end.

    For example:
    GTM>set file="myFile.dat"
    GTM>open file:(NEW:STREAM:NOWRAP)
    GTM>use file write "abc"
    GTM>close file
    GTM>zsystem ls -al myFile.dat
    GTM>zsystem "ls -al myFile.dat"
    -rw-rw-r--   1 aussen  alster         4 Aug 25 21:48 myFile.dat
    Although I have only written 3 characters to the file the file length is 4.  The last character is a linefeed $c(10) which I don't want.

    This may be a bug, but from what I can tell from the docs it may be by design.  Whatever, I want to create a file that is not terminated by a linefeed.

    Does anyone know a way of doing this?
    Thanks.

     
    • Dr. Martin Lehr

      Dr. Martin Lehr - 2002-08-26

      Hi George,

      some times ago I had to deal with the same problem.
      It seems to be not a bug, because stream files in UNIX are delimited and GT.M does not
      leave an undelimited record at the end of the file and adds an extra $C(10).

      So, how to create a file without $C(10) at the end of the file?
      I found the following (one can say, ugly) solution:

      S F="test.dat"
      O F:(NEWVERSION:WRAP:FIXED:RECORDSIZE=1:BLOCKSIZE=1):1 I '$T Q
      U F
      W "abc"
      C F
      ZSY "ls -al test.dat"
      Q

      This works with GTM 4.2-002. I don't know if it works with
      GTM 4.3, I have not yet tested it. Disadvantage:
      you have to replace constructions like W "abcde",!
      with W "abcde",$C(10)

      By the way: It is the Close that adds the extra $C(10).
      So it is also possible to do the following:

      S F="test.dat"
      O F:(NEWVERSION:STREAM:NOWRAP):1 I '$T Q
      W "abc"
      ZSY "cp test.dat test1.dat"
      C F
      ZSY "ls -al test1.dat"
      Q

      Regards,
      Martin

       
    • George James

      George James - 2002-08-26

      Martin
      Thank you.

      I think I prefer the FIXED:BLOCKSIZE=1:RECORDSIZE=1:WRAP approach rather than renaming just before it is closed.

      It would be nice to have a more intuitive Open argument that does this.

      I can confirm that this does work on GTM 4.3-001B.

      Regards
      George

       
    • John Murray

      John Murray - 2002-08-26

      Try issuing the following command just before you CLOSE file:

      SET $X=0

      That should fool GT.M into thinking there's no need to add the final line-terminator when you CLOSE the file.

      John

       
    • Dr. Martin Lehr

      Dr. Martin Lehr - 2002-08-26

      Hi John,

      I just tried out $X=0 before close -
      it works perfectly!

      I also must say that the BLOCKSIZE=1 - trick results in slow performance on floppy disks when the floppy is set to sync in fstab

      So the $X=0 before close should be the better
      solution

      Thanks
      Martin

       
    • Sid

      Sid - 2008-03-13

      I think there's another similar issue when writing to sequential files:

      Try this code:

      GTM>S file="myFile.dat"
      GTM>O file:(NEW:STREAM:NOWRAP)
      GTM>U file W "abc",#
      GTM>C file
      GTM>ZSYS "ls -l myFile.dat"
      -rw-rw-rw- 1 sid dba 6 Mar 13 12:43 myFile.dat

      As you can see, the length is 6 instead of 4 as expected. When I take a look at the content, I get "abc" + $C(10,12,10). GT.M wraps the FormFeed character $C(12) with 2 LineFeeds $C(10).
      I noticed that if I change th Write statement to W "abc",*12 it does the job and adds only the $C(12) as expected.
      If I change the Open parameters to NEW:WRAP:FIXED:RECORDSIZE=1:BLOCKSIZE=1
      and let the Write statement as before: W "abc",# it works fine as well, but using these paremeters in large file coud slower performance.

      Is there a better way to avoid the $C(10) characters without changing the original code W "abc",#  ?

       

Log in to post a comment.