Thread: Reading File

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    47

    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!

    Regards,
    Robert

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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 ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    47
    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.

    Regards,
    Robert

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    47
    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.

    Regards,
    Robert

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    >i get some typecast errors.

    The code looks fine, can you post them, I don't have a compiler on this system.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    47
    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

    Regards,
    Robert

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM