-
Reading File
Hi all,
i'm working with Borland C# and want to load a binary file into memory.
Code:
private void m_Open_Click(object sender, System.EventArgs e)
{
if ( openFileDialog.ShowDialog() == DialogResult.OK )
{
FileStream fs = new FileStream(openFileDialong.FileName, FileMode.Open);
if (fs.CanRead)
{
}
else
{
}
}
else
{
}
My question is how do i define a varaible that hold my "pointer" to the memory location and where is the correct place where the varaible should appeaer. I'm confused of all these Reader classes,
which is the best and most efficeint for it ?
Thanks!
-
Maybe a flat block of memory isn't the best way to hold the contents of your file. There are a lot of classes that organize objects such as stacks, lists, arrays, hashes etc.
What kind of data do you want to load ?
-
nvoigti want a simple binary file. This file will then be identified by some tags and headers.
Example:
0000000000E00000 ..... upto 4MB filledup with data.
This file would tell me then that i have only to load 128KB of data
and that this is a ROM file.
So a flat block of memory would be the best solution for me then.
-
i've tested this code:
Code:
.
.
private byte[] fileMem;
.
.
FileStream fs = new FileStream (openFileDialog.FileName, FileMode.Open);
if (fs.CanRead)
{
fileMem = new byte[fs.Length];
fs.Read(ref fileMem, (int)0, (int)fs.Length);
}
i get some typecast errors.
-
>i get some typecast errors.
The code looks fine, can you post them, I don't have a compiler on this system.
-
hello,
in fact the typecast errors disappeared with this version.
i've posted the previous message while i was changing
the fs.Read .... line.
Code:
private void m_Open_Click(object sender, System.EventArgs e)
{
if ( openFileDialog.ShowDialog() == DialogResult.OK )
{
FileStream fs = new FileStream (openFileDialog.FileName, FileMode.Open);
if (fs.CanRead)
{
fileMem = new byte[fs.Length];
fs.Read(fileMem, (int)0, (int)fs.Length);
BinIdent.IdentifyBinary (fileMem);
if (BinIdent.typeID > 0)
{
// call heavy class
}
else
{
//
}
fs.Close ();
}
else
{
MessageBox.Show ("The choosen file couln't be loaded!!!", "MaDis Error ...",MessageBoxButtons.OK , MessageBoxIcon.Error);
}
}
else
{
}
}
the BinIndent class looks like this.
Code:
using System;
namespace madis
{
/// <summary>
/// Zusammenfassende Beschreibung für Class
/// </summary>
public class BinIdent
{
public BinIdent()
{
}
public static int typeID = (int)BinaryType.RAW;
public static int IdentifyBinary(byte[] memptr)
{
#region 64BIT ID Test
ulong _64bitID = 0;
for (int i=0; i<8; i++)
{
_64bitID <<= 8;
_64bitID |=memptr[i];
}
switch (_64bitID)
{
// Atari Jaguar ROM File (128 KB)
case 0xE00008:
typeID = (int)BinaryType.JAGUAR_BOOTROM;
break;
case 0xF61A8C5FF028D5C2:
typeID = (int)BinaryType.JAGUAR_CDBOOTROM;
break;
default:
typeID = 0;
break;
}
#endregion
if (typeID != (int)BinaryType.RAW)
return typeID;
return typeID;
}
}
}
i'm wondering that the numbers in C# are big-endian however.
Thanks