PxPlus User Forum
Main Board => Discussions => Programming => Topic started by: Loren Doornek 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
-
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
-
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.
-
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?
-
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.