Connecting Tech Pros Worldwide Forums | Help | Site Map

How to efficiently read Random Access Files ?

Cybertof
Guest
 
Posts: n/a
#1: Nov 15 '05
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.

Horatiu Ripa
Guest
 
Posts: n/a
#2: Nov 15 '05

re: How to efficiently read Random Access Files ?


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" <cybertof2003nospam@gmx.net> wrote in message
news:MPG.1a09865fe4b70529896bc@msnews.microsoft.co m...[color=blue]
> 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.[/color]


Cybertof
Guest
 
Posts: n/a
#3: Nov 15 '05

re: How to efficiently read Random Access Files ?


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 <uwUuhlinDHA.2500@TK2MSFTNGP10.phx.gbl>, unreal@businessco.us
says...[color=blue]
>Console.WriteLine(p->integerValue);
>
>Console.WriteLine(p->charValue);[/color]
Horatiu Ripa
Guest
 
Posts: n/a
#4: Nov 15 '05

re: How to efficiently read Random Access Files ?


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" <cybertof2003nospam@gmx.net> wrote in message
news:MPG.1a09fc3c2ecfad039896bd@msnews.microsoft.c om...[color=blue]
> 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 <uwUuhlinDHA.2500@TK2MSFTNGP10.phx.gbl>, unreal@businessco.us
> says...[color=green]
> >Console.WriteLine(p->integerValue);
> >
> >Console.WriteLine(p->charValue);[/color][/color]


Closed Thread