473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to efficiently read Random Access Files ?

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 ? Maybe a unmanaged C function call....but isn't
there any overhead in call unmanaged code ?

What would be the best efficient way to do this ?

I have to read 50.000 records having this VB6 structure :
Type StickStruct
dte As Date
open As Long
High As Long
Low As Long
close As Long
Volume As Long
openinterest As Long
End Type
I would appreciate any advice or comment.

Regards,
Cybertof.
Nov 15 '05 #1
3 5274
using System;

namespace ConsoleApplication1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

public struct MyStructure

{

public int integerValue;

public char charValue;

}
class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

class UnsafeTest

{

// unsafe method: takes pointer to int:

[STAThread]

unsafe public static void Main()

{

byte[] buffer = {0xFF,0xFF,0xFF,0x7F,0x66};

fixed(byte* pArray = buffer)

{

MyStructure* p = (MyStructure*)(pArray);

Console.WriteLine(p->integerValue);

Console.WriteLine(p->charValue);

Console.ReadLine();

}

}

}

}

}
--
Horatiu Ripa

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP***********************@msnews.microsoft.co m...
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 ? Maybe a unmanaged C function call....but isn't
there any overhead in call unmanaged code ?

What would be the best efficient way to do this ?

I have to read 50.000 records having this VB6 structure :
Type StickStruct
dte As Date
open As Long
High As Long
Low As Long
close As Long
Volume As Long
openinterest As Long
End Type
I would appreciate any advice or comment.

Regards,
Cybertof.

Nov 15 '05 #2
How would you do the same to write the entire structure to a random
access file (so using a C filesystem call as there is no api call in
..net to write directly a structure content) ?

Thanks,
Cybertof.

In article <uw**************@TK2MSFTNGP10.phx.gbl>, un****@businessco.us
says...
Console.WriteLine(p->integerValue);

Console.WriteLine(p->charValue);

Nov 15 '05 #3
I'm not sure what you really want to do. Anyhow here's some hints:

If you want to serialize/deserialize an object, BOTH ACTIONS IN .Net, in a
binary file here's an example:
using System;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

namespace ConsoleApplication2

{

/// <summary>

/// Summary description for Class1.

/// </summary>

[Serializable]

public struct Point

{

private int x, y;

// Constructor:

public Point(int x, int y)

{

this.x = x;

this.y = y;

}

public int xValue

{

get {return this.x;}

set {this.x = value;}

}

public int yValue

{

get {return this.y;}

set {this.y = value;}

}

// Override the ToString method:

public override string ToString()

{

return(String.Format("(x,y)= " + x + "," + y));

}

}


class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

Point p = new Point(10,15);

Console.WriteLine(p.ToString());

p.xValue = 3;

p.yValue = 4;

Console.WriteLine(p.ToString());
FileStream fs = new FileStream("d:/kk.bin",FileMode.Create);

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(fs,p);

fs.Close();

p.xValue = 12;

p.yValue = 13;

Console.WriteLine(p.ToString());

fs = new FileStream("d:/kk.bin",FileMode.Open);

p = (Point)(formatter.Deserialize(fs));

Console.WriteLine(p.ToString());

Console.ReadLine();

}

}

}

That eases your effort, but it can't be deserialized in other environments.
If you want to have compatibility with all other environments you can use
the sequence of code I've already gived you; it works both ways, the byte[]
buffer can be read/write through a Stream from/to a file.

--
Horatiu Ripa

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
How would you do the same to write the entire structure to a random
access file (so using a C filesystem call as there is no api call in
.net to write directly a structure content) ?

Thanks,
Cybertof.

In article <uw**************@TK2MSFTNGP10.phx.gbl>, un****@businessco.us
says...
Console.WriteLine(p->integerValue);

Console.WriteLine(p->charValue);

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Russell Mangel | last post by:
What would be the best way to parse this XML document? I want to avoid using XMLDocument. I don't know if I should use XMLTextReader, or Xpath classes. There is only one element <MessageStore>...
5
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...
3
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...
3
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library...
31
by: krypto.wizard | last post by:
How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer.
39
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...
7
by: random guy | last post by:
Hi, I'm writing a program which creates an index of text files. For each file it processes, the program records the start and end positions (as returned by tellg()) of sections of interest,...
3
by: Laphan | last post by:
Hi All I use a MySQL DB with my ASP classic web app. I've been asked if I can create a routine whereby I get a random number of products (records) from the DB and display these on the site. ...
1
by: Steven D'Aprano | last post by:
On Wed, 22 Oct 2008 16:59:45 -0400, Terry Reedy wrote: With respect Terry, I think what you have missed is the reason why the OP thinks this is a bug. He's not surprised that buffering is going...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.