writing bytes from file directly to memorry

Shurikn

Well-known member
Joined
Jul 14, 2004
Messages
60
I have a C++ code that do this(write from a file directly to memory)
it reads a wave file and store it in memory so it can be changed... heres my Wave.h file:
C#:
struct EnteteWave
{
	char chunkID [4];
	int chunkSize;
	char format [4];
	char subchunk1ID [4];
	int subchunk1Size;
	short audioFormat;
	short numChannels;
	int sampleRate;
	int byteRate;
	short blockAlign;
	short bitsPerSample;
	char subchunk2ID [4];
	int subchunk2Size;
};
class FichierWave
{
public:
	FichierWave();
	~FichierWave();
	int LireFichier(char *nomFic);
	int EcrireFichier(char *nomFic); 
	double CalculerDuree();
	short GetNbCanaux();//number of chanel
	int GetFreqEch();
	short GetNbBitsPerEch();
	int GetByteRate();
	short *donneeG;//left channel data
	short *donneeD;//right chanel data
private:
	EnteteWave *entete;
	int nbSamples;
	char *nomFichier;
};

I reand from the file and write to memory using this code:

C#:
int FichierWave::LireFichier(char *nomFic)
{
	nomFichier = nomFic;
	FILE *fp;
	fp = fopen(nomFichier, "rb");
	if (! fp)
		return 1;
	int validation = fread(entete, sizeof(EnteteWave), 1, fp);
	if (validation != 1)
		return 2;
	if (strncmp(entete->chunkID,"RIFF", 4) != 0)
	{
		fclose(fp);
		return 3;
	}
	if (strncmp(entete->format, "WAVE", 4) != 0)
	{
		fclose(fp);
		return 4;
	}
	if (strncmp(entete->subchunk1ID, "fmt ", 3) != 0)
	{
		fclose(fp);
		return 5;
	}
	if (entete->audioFormat != 1)
	{
		fclose(fp);
		return 6;
	}
	if (strncmp(entete->subchunk2ID, "data",4) != 0)
	{
		fclose(fp);
		return 7;
	}
	if (entete->subchunk2Size <= 0)
	{
		fclose(fp);
		return 8;
	}
...
...

I tryed to code something similar in C#, but I dont think itll work because first: to replace char I used byte, but if I say:
C#:
if(entete.chunkID=="RIFF")
will it see the Bytes as an array of char?... here how i trye to do it in c#:
((actually in my c# program I try to read trough all visible media types so heres the exemple of a jpg))
C#:
[StructLayout(LayoutKind.Sequential)]
	public struct EnTeteJPG
	{
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
		public byte[] SOI;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
		public byte[] JFIFMarker;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
		public byte[] length;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=5)]
		public byte[] identifier;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
		public byte[] version;
		public byte units;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
		public byte[] Xdensity;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
        public byte[] Ydensity;
        public byte Xthumbnail;// 0 = no thumbnail
		public byte Ythumbnail;// 0 = no thumbnail
	}
I try to write it that way:
C#:
public int LireFichier(string nomFichier)
		{
			this.nomFichier=nomFichier;
			FileStream stream=new FileStream(nomfichier,FileMode.Open);
			BinaryReader reader=new BinaryReader(stream);
			entete=reader.ReadByte(sizeof(EnTeteJPG));
			reader.Close();
			stream.Close();

			if(entete.SOI!=&HFFD8)
			{
				return 0;
			}
			if(entete.JFIFMarker!=&HFFE0)
			{
				return 0;
			}
			if(entete.identifier!=&H4A46494600)
			{
				return 0;
			}
			return 1;
		}
i used various exemple from this site to help me write this code (mainly the marshalling things, which by the way dont work yet....)

so I am close to doing this right? I saw that there is a way to use pointers in c#, maybe I could try with that... anyone can help?
 
As I suspected, this code is not working I had to change this:
C#:
this.nomFichier=nomFichier;
			FileStream stream=new FileStream(nomfichier,FileMode.Open);
			BinaryReader reader=new BinaryReader(stream);
			entete=reader.ReadByte(sizeof(EnTeteJPG));
			reader.Close();
			stream.Close();

to this:
C#:
this.nomFichier=nomFichier;
			FileStream stream=new FileStream(nomFichier,FileMode.Open);
			BinaryReader reader=new BinaryReader(stream);
			entete=reader.ReadByte(Marshal.SizeOf(EnteteAVI));
			reader.Close();
			stream.Close();

because he didnt seem to like me useing SizeOf, the compilator was saying to use Unsafe or marshal.SizeOF
so i chosed Marshal.SizeOf, but now, it tells me the the Marshal.SizeOf is expecting an object and not a class, but thats not how it work in c++...

the other error I get are this:
C#:
if(entete.chunkId!=Convert.ToByte("RIFF"))
{
	return 0;
}
I get the error:
Code:
C:\Documents and Settings\Nicolas Dufour\Mes documents\Visual Studio Projects\ZoneMedia\AVI.cs(27): Operator != cannot be applied to operands of type byte[] and byte

so as I suspected, he dont see an array of byte as chars... so I dont really know how to handle this here... please anyone can help?
 
Your post was big and it hurt my head. I hope these might help: To convert a string to bytes with one byte per character (like a C++ char), use the System.Text.ASCIIEncoding class. There are tutorials that can be found with google that show how to read/write structs to a file using the FileStream class. This example is in VB but it should be easy to translate to C#.
 
Thank you! and sorry for hurting your head like that, but I wanted my message to be as clear as possible and dont have people asking me: can you post this or post that
 
Back
Top