How to efficiently read Random Access Files ?

  • Thread starter Thread starter Cybertof
  • Start date Start date
C

Cybertof

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.
 
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();

}

}

}

}

}
 
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.
 
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.
 
Back
Top