471,570 Members | 934 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Random Access Files

In VB6 I could use random access and fixed record lengths to read specific
records from a test file. Eg the following code would save a LogEntry to a
file at the location specified by RecordNumber.

Private Type LogEntry
Date As String * 20
Number As String * 20
Source As String * 100
Description As String * 100
CRLF As String * 2
End Type
Public Sub RecordError(Num As Long, Source As String, Desc As String)
Dim FF As Integer
Dim CurrentRecord As LogEntry
'set the record values
With CurrentRecord
.Date = Trim(Format$(Now, "DD/MM/YYYY HH:MM:SS"))
.Number = Num
.Source = Trim(Source)
.Description = Trim(Desc)
.CRLF = vbCrLf
End With
'save the record
FF = FreeFile
Open "file.txt" For Random As FF Len = 242
Put FF, RecordNumber, CurrentRecord
Close #FF
End Sub

How do I do this sort of thing in Csharp? I can't seem to find a way to
read/write fixed sized records.

Thanks

Alan
Dec 20 '05 #1
1 2273
Alan,

There really isn't a simple way to do this.

The best way to do this would be to declare the LogEntry class with the
properties that you specify. Then, I would have a method which would
perform the serialization of the items into a byte array which is 242 bytes
long.

Then, you would have a method which would open the file, move the file
pointer to the appropriate location, write the record, and then close it.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alan Roberts" <al**@statistixl.co.uk> wrote in message
news:Oh**************@tk2msftngp13.phx.gbl...
In VB6 I could use random access and fixed record lengths to read specific
records from a test file. Eg the following code would save a LogEntry to
a file at the location specified by RecordNumber.

Private Type LogEntry
Date As String * 20
Number As String * 20
Source As String * 100
Description As String * 100
CRLF As String * 2
End Type
Public Sub RecordError(Num As Long, Source As String, Desc As String)
Dim FF As Integer
Dim CurrentRecord As LogEntry
'set the record values
With CurrentRecord
.Date = Trim(Format$(Now, "DD/MM/YYYY HH:MM:SS"))
.Number = Num
.Source = Trim(Source)
.Description = Trim(Desc)
.CRLF = vbCrLf
End With
'save the record
FF = FreeFile
Open "file.txt" For Random As FF Len = 242
Put FF, RecordNumber, CurrentRecord
Close #FF
End Sub

How do I do this sort of thing in Csharp? I can't seem to find a way to
read/write fixed sized records.

Thanks

Alan

Dec 24 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Oin Zea | last post: by
9 posts views Thread by greeningster | last post: by
3 posts views Thread by Cybertof | last post: by
1 post views Thread by Patrick | last post: by
3 posts views Thread by Simon | last post: by
13 posts views Thread by Stuart | last post: by
16 posts views Thread by Claudio Grondi | last post: by
39 posts views Thread by Alan Isaac | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.