PxPlus User Forum

Twitter Twitter Twitter

Author Topic: How to get Filelength for an open serial file  (Read 1230 times)

Loren Doornek

  • Gold Member
  • ****
  • Posts: 85
    • View Profile
How to get Filelength for an open serial file
« on: March 10, 2020, 10:35:05 AM »
Is there any way to get the file size (filelength) for an open serial file that is being written to?  The fin(chan,"filelength") doesn't seem to know the length until after the file is closed, but I need to know the length of the file while I'm writing to it, so that I can restrict the size.

For example, I wrote 100 bytes to a serial file, and would like to know that it is 100 bytes at any time.  But, the filelength returns zero until I close and re-open the file.

-}serial "temp.txt"
-}open lock(1)"temp.txt
-}print(1)dim(100,"X")
-}print fin(1,"filelength")
0
-}close(1);open(1)"temp.txt";print fin(1,"filelength");close(1)
101

chrisk

  • Member
  • **
  • Posts: 14
    • View Profile
Re: How to get Filelength for an open serial file
« Reply #1 on: March 10, 2020, 11:11:49 PM »
Loren,

At the risk of decreased performance, you could turn off output buffering.

->! Sample one
->serial "temp.txt"
->open lock(1)"temp.txt"
->print(1)dim(100,"X")
->print fin(1,"filelength")
0
->close(1);open(1)"temp.txt";print fin(1,"filelength");close(1)
101
->erase "temp.txt"


->! Sample two
->serial "temp.txt"
->open lock(1)"temp.txt"
->print (1)'-B',
->print(1)dim(100,"X")
->print fin(1,"filelength")
101
->close(1);open(1)"temp.txt";print fin(1,"filelength");close(1)
101
->erase "temp.txt"

HTH
Chris Kukuchka
Sequoia Group, Inc.

Mike King

  • Diamond Member
  • *****
  • Posts: 3811
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: How to get Filelength for an open serial file
« Reply #2 on: March 11, 2020, 08:56:13 AM »
Did you know that SERIAL files have a logical 4 byte key for each record?  That key is the binary representation of the physical record location in the file (byte offset).

So given this, if all you want to know is where you are when outputing to a serial file simply check KEC() for the file.

For Example:

->serial "mar11
->open purge (1) "mar11"
->write record (1) "Mike was here"
->print dec(kec(1))
 15


That's thirteen bytes for "Mike was here" and two bytes for the trailing CR/LF.
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

James Zukowski

  • Diamond Member
  • *****
  • Posts: 297
    • View Profile
Re: How to get Filelength for an open serial file
« Reply #3 on: March 11, 2020, 10:18:12 AM »
I didn't know that.

Does that also mean that the KEL() function would actually return the end-of-file position, as opposed to the KEC(), which returns the current position?
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services

Mike King

  • Diamond Member
  • *****
  • Posts: 3811
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: How to get Filelength for an open serial file
« Reply #4 on: March 11, 2020, 02:40:52 PM »
No, we only implement KEC to return the current position (record offset which is about to read or position where next record will be written),

However you can position yourself in the file using the KEY= clause and a 4 byte logical key value.
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com