FilePut vs FilePutObject

kejpa

Well-known member
Joined
Oct 10, 2003
Messages
320
Hi there,
I have a general log-class which I use for logging a number of different structures.
Code:
	Public Sub Add2Log(ByVal LogItem As Object, Optional ByVal TimeStamp As Double = -1)
<snip>
	FilePutObject(iFilNr, LogItem, iWritePos)
	iWritePos += Len(LogItem)
	FilePut(iFilNr, iWritePos, 1)
<snip>
End Sub
One of the things I log is the following Struct
Code:
Public Structure CanLoggFormat
	Public Nodid As Byte	    1-127
	Public SyncTime As Integer   ms since OS started rollover every 49 days
	Public Value1 As Short	    +/-30000
	Public Value2 As Short	    +/-30000
	Public Value3 As Short	    +/-30000
End Structure
When I try to write this to a file I get
System.ArgumentException: FilePutObject of structure CanLoggFormat is not valid. Use FilePut instead.
But if I try to change the line to
FilePut(iFilNr, LogItem, iWritePos)
it wont compile
Public Overloads Sub FilePut(FileNumber As Object, Value As Object, [RecordNumber As Object = -1]) is obsolete: Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types

FYI, this is not the only Struct I plan to log. I might even try to log some class-object. Thats why I used LogItem as Object

I stand as confused as a haytap between two donkeys

Any suggestions?
/Kejpa
 
Are you looking to output these objects in a readable format or is it something you will need to load back in later?
Also I would recomend moving away from the old VB6 File based functions (FilePut, FileOpen etc) and look at using the classes under System.IO instead.
 
PlausiblyDamp said:
Are you looking to output these objects in a readable format or is it something you will need to load back in later?
Also I would recomend moving away from the old VB6 File based functions (FilePut, FileOpen etc) and look at using the classes under System.IO instead.
Yes, I will read them back later, I keep each structure in its own file so knowing which structure is stored is no problem.

Id love to use the new File functions but I havent got them working yet. And I havent got any good samples to follow.
The files basically looks like this:
pos 1-4. Next write pos (integer)
pos 5-105 Fileinformation (string)
pos 106- Saved structs

Any help is appreciated :)
/Kejpa
 
PlausiblyDamp said:
Heres a quick sample of using serialization and the new file I/O methods.
Monday and the rain is pouring down :)

About your sample,
Theres no information about what record youre reading. Every call to the read function will render the same result, the first record, right?
The load array (which dont seems to work) fetches the whole damn file, doesnt it?
I need to be able to read each record one at a time and get the next the next time I call the function. The file will have at least two records written evey 300ms, so in a day there will be quite a few records to read... (I plan to keep each day in a separate file)

I also need to have a customized header in the file. Readable for humans using "Notepad" to get some info on the system where the log is created. But I guess I can write it to the file in the sub where the file is created.

Many thanx again
/Kejpa
 
Last edited by a moderator:
You never actually stated you needed to be able to read them back individually - so the code I threw together just shows how to use serialization in a simple fashion. If you want to read elements sequentially using serialization then dont close the file after each read and it will maintain its position in the file.
The loading of the array certainly works here - its just that I didnt bother to give every single array element a new value, you should see element 2 was loaded correctly though. In the case given as the file only contains an array then yes it will load the entire file again.

If you need more control over the actual file format then you probably want to investigate the BinaryReader, BinaryWriter and FileStream classes.
 
Back
Top