Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi there,

I have a general log-class which I use for logging a number of different structures.

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

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 won't 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. That's why I used 'LogItem as Object'

 

I stand as confused as a haytap between two donkeys

 

Any suggestions?

/Kejpa

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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 it's own file so knowing which structure is stored is no problem.

 

I'd love to use the new File functions but I haven't got them working yet. And I haven't 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

Posted (edited)
Here's a quick sample of using serialization and the new file I/O methods.

Monday and the rain is pouring down :)

 

About your sample,

There's no information about what record you're reading. Every call to the read function will render the same result, the first record, right?

The load array (which don't seems to work) fetches the whole damn file, doesn't 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

Edited by kejpa
  • Administrators
Posted

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 don't close the file after each read and it will maintain it's position in the file.

The loading of the array certainly works here - it's just that I didn't 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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...