473,581 Members | 2,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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$(No w, "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 2340
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.co m

"Alan Roberts" <al**@statistix l.co.uk> wrote in message
news:Oh******** ******@tk2msftn gp13.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$(No w, "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6999
by: Oin Zea | last post by:
Is it possible for to program to access a random file at the same time and perform actions like create a new record?
9
3835
by: greeningster | last post by:
I have written an application in Visual C++ for a customer but it seems to crash randomly. Could anyone give me any help on how I could track this down ? Also, there appears there might be memory leaks too. How can i track these down ?
3
10189
by: Cybertof | last post by:
Hello, Is there a simple way to read a random access file that has been created with VB6 using direct writing to disk of Type....End Type structures ? I have not found this possibility in C#. Is it possible to map directly a buffer (read from a stream) to a Structure in memory ? If yes, how to do this ? If no, any hint to...
1
2377
by: Patrick | last post by:
Hi, This post is the 'sequel' ;) of the "Data Oriented vs Object Oriented Design" post, but it can be read and treated apart from that one. I will just quote the beginning of my previous message to expose the problem: This post deals with type (2) files.
5
2937
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times I try to start to run/debug a project (through Visual Studio) Server Error in '/Intranet' Application. ...
3
3818
by: Simon | last post by:
This problem has been driving me mad for months.... Seen a few posts on forums about it but no answers... No mention on MSDN etc. XP Pro SP1, VS.NET (c#) .Net framework 1.1, IIS 5.1. In a nutshell when testing my ASP.NET (localhost) apps images randomly don't load on the page. Examining the IIS logfile shows the missing images give 401 or...
13
2562
by: Stuart | last post by:
I have converted a VB6 app to VB.NET. It's function is to generate reports from a Random Access file but the .NET version is pathetically slow compared to the VB6 version. I think I need to to get the FileStream class to work with Random Access Files, but it requires the data Structures to be in a byte array. How do I convert them? S.
16
7170
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain unchanged). How can I do that in Python? Claudio Grondi
39
2444
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and MyClass from module2. module2 does not import random. module1 sets a seed like this::
5
2401
by: Peter | last post by:
Hi I will use a Random Access File in dotnet/csharp. The file is created with Visual Basic 6 (VB6). My Problem is to find out the corresponding Types I had to use in dotnet - reading the VB6 binary values. If there are no corresponding Types I will manually convert the VB6 binary values (e.g. BinaryFormatter...). Where can I find the
0
7854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8296
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8157
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6539
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5350
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3807
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.