OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Loc(FileNum)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
bertram
Power User
Power User


Joined: 13 Nov 2005
Posts: 55

PostPosted: Sun Aug 19, 2007 12:35 pm    Post subject: Loc(FileNum) Reply with quote

I'm trying to use Loc() to get a number I need:-- that is number of records in a CSV file, where records may be from 3 chars to 60.
As a test, I open a 500 record CSV file for append, print a line to the file (which it does) and ask for Loc()
I get 0. What's wrong?
Code:
Sub case5 (Ticks(), D As Double)
Ticks(0) = GetSystemTicks()
Dim dataFileName as String, FileNum As Integer
Dim Records As Integer
Dim A As Double, B As Double, C As Double
dataFileName = "file:///E:/temp/00working/dataFile_csv500.txt"
FileNum = Freefile
Open dataFileName For Append As #FileNum
'Print #FileNum, "ok, it's appending"  'check of file shows it's appending
REM the problem...
Records = Loc(FileNum)  '<== Records = 0! after writing to line 501
Close #FileNum
Dim dataArray(0 to Records, 0 to 2) As Double
#FileNum = Freefile
Open Filename For Input As #FileNum
For row = 0 to Records
   Input #FileNum, dataArray(row, 0), dataArray(row, 1), dataArray(row, 2)
next row
For row = 0 to Records
   D = dataArray(row, 0) + dataArray(row, 1) + dataArray(row, 2)  'insignificant crunching
next row
ReDim dataArray(0 to 0, 0 to 2) As Double
Close #FileNum
Ticks(1) = GetSystemTicks()
Print "ticks=" + (Ticks(1) - Ticks(0))
End Sub
Back to top
View user's profile Send private message
keme
Moderator
Moderator


Joined: 30 Aug 2004
Posts: 2910
Location: Egersund, Norway

PostPosted: Sun Aug 19, 2007 1:14 pm    Post subject: Reply with quote

Some thougths. I'm too lazy to try them out myself, though...

For flat files with variable record length, I believe you need to parse the entire file to count records. You should be able to get the byte count, though, but there may still be some pitfalls:

The use of the print statement for file IO is not documented in OOoBasic help. It does work, it seems, but maybe file handle pointers aren't updated correctly.
Try using write instead.

Opening for append may consider the workspace to start at current eof, so you're always at position 0. Try opening for output rather than append, then use the seek statement to get to end of file before writing.

Use the seek() function to determine current position. Looks like it does the same as loc()...

Use the lof() function to determine file size.

Use the FileLen() function to determine file size. Note that this works on file system content, not on file handles for open files, so the result for open files may be unreliable.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group