Programming: C++ Binary IO Structure(More Like .net IO)

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
TfAv1228




Miner Logistician Droplet

Posts: 276
Joined: Sun Sep 05, 2004 4:40 pm

Programming: C++ Binary IO Structure(More Like .net IO)

Post by TfAv1228 »

this code will help you to make binary file handeling in c++ easier and more like C#.net or VB.net

Code: Select all

#include <stdio.h>

struct BinaryRW
{
	FILE *BinRW;
	void Open(const char *filename)
	{
		BinRW=fopen(filename, "r+b");
	}
	void Close()
	{
		fclose(BinRW);
	}
	void Seek(int position, int fromwhere=0)
	{	
		// 0 = From Start - 1 = From Current - 2 = From End
		fseek(BinRW, position, fromwhere);
	}
	int GetPosition()
	{
		return ftell(BinRW);
	}
	int GetFileLength()
	{
		int pos=GetPosition();
		Seek(0, 2);
		int end=GetPosition();
		Seek(pos);
		return end;
	}
	int ReadInt32()
	{
		int tmp;
		fread(&tmp, 4, 1, BinRW);
		return tmp;
	}
	int ReadShort()
	{
		int tmp;
		fread(&tmp, 2, 1, BinRW);
		return tmp;
	}
	int ReadByte()
	{
		int tmp;
		fread(&tmp, 1, 1, BinRW);
		return tmp;
	}
	char *ReadChars(int size)
	{
		char *tmp;
		fread(&tmp, size, 1, BinRW);
		return tmp;
	}
	void WriteInt32(int theint)
	{
		fwrite(&theint, 4, 1, BinRW);
	}
	void WriteShort(int theshort)
	{
		fwrite(&theshort, 2, 1, BinRW);
	}
	void WriteByte(int thebyte)
	{
		fwrite(&thebyte, 1, 1, BinRW);
	}
	void WriteChars(const char* thechars, int thesize)
	{
		fwrite(&thechars, thesize, 1, BinRW);
	}
};
How to use

Code: Select all

BinaryRW TheBRW;
BinaryRW TheBRW2;
TheBRW.Open("C:/example.bin");
TheBRW2.Open("C:/example2.bin");
TheBRW.Seek(8, 0);
TheBRW2.Seek(8, 0);
TheBRW2.WriteInt32(TheBRW.ReadInt32());
TheBRW.Close();
TheBRW2.Close();
That code above would read an int from 0x8 in example.bin and write it to 0x8 in example2.bin
Sig breaks rules, read the rules before reposting.
twolvesfan369





Posts: 16
Joined: Wed Jul 20, 2005 4:15 pm

Post by twolvesfan369 »

nice..u never fail to amaze me..
User avatar
kornman00




ONI New Age

Posts: 146
Joined: Fri Dec 12, 2003 6:30 pm
Contact:

Post by kornman00 »

A better implementation of it, to mimic .NET would be

Code: Select all

#include <stdio.h>
#include <string>

typedef void* Object;
typedef unsigned char Byte;
typedef signed char SByte;
typedef unsigned short UInt16;
typedef signed short Int16;
typedef unsigned long UInt32;
typedef signed long Int32;
typedef unsigned __int64 UInt64;
typedef signed __int64 Int64;
typedef const char* String;

typedef enum t_SeekOrigin
{
	SeekOriginBegin,
	SeekOriginCurrent,
	SeekOriginEnd,
}SeekOrigin;

class BinaryStream
{
private:
	FILE *BinRW;

public:
	BinaryStream(String filename)
	{
		this->Open(filename);
	}

	~BinaryStream()
	{
		delete BinRW;
	}

	FILE* BaseStream()
	{
		return BinBW;
	}

	void BaseStream(FILE* stream)
	{
		if(BinRW)
			BinRW = stream;
	}

	void Open(String filename)
	{
		if(BinRW)
		{
			this->Flush();
			this->Close();
			BinRW = NULL;
		}

		BinRW = fopen(filename, "r+b");
	}

	void Close()
	{
		fclose(BinRW);
	}

	void Flush()
	{
		fflush(BinRW);
	}

	void Seek(Int32 position, SeekOrigin origin)
	{   
		fseek(BinRW, position, origin);
	}

	Int32 Position()
	{
		return ftell(BinRW);
	}

	void Position(long pos)
	{
		this->Seek(pos, SeekOrigin::SeekOriginBegin);
	}

	Int32 FileLength()
	{
		Int32 pos = GetPosition();
		this->Seek(0, 2);
		int end = GetPosition();
		this->Seek(pos);
		return end;
	}

	template<class T> T ReadObjectByValue()
	{
		T value;
		fread(&value, sizeof(T), 1 BinBW);
		return value;
	}

	template<class T> T* ReadObjectByReference()
	{
		T* value;
		fread(value, sizeof(T), 1 BinBW);
		return value;
	}

	Int64 ReadInt64()
	{
		Int64 value;
		fread(&value, sizeof(Int64), 1, BinRW);
		return value;
	}

	UInt64 ReadUInt64()
	{
		UInt64 value;
		fread(&value, sizeof(UInt64), 1, BinRW);
		return value;
	}

	Int32 ReadInt32()
	{
		Int32 value;
		fread(&value, sizeof(Int32), 1, BinRW);
		return value;
	}

	UInt32 ReadUInt32()
	{
		UInt32 value;
		fread(&value, sizeof(Int32), 1, BinRW);
		return value;
	}

	Int16 ReadInt16()
	{
		Int16 value;
		fread(&value, sizeof(Int16), 1, BinRW);
		return value;
	}

	UInt16 ReadUInt16()
	{
		UInt16 value;
		fread(&value, sizeof(UInt16), 1, BinRW);
		return value;
	}

	Byte ReadByte()
	{
		Byte value;
		fread(&value, sizeof(Byte), 1, BinRW);
		return value;
	}

	SByte ReadSByte()
	{
		SByte value;
		fread(&value, sizeof(SByte), 1, BinRW);
		return value;
	}

	char *ReadChars(int size)
	{
		char* value;
		fread(&value, size, 1, BinRW);
		return value;
	}

	template<class T> void WriteObjectByValue(T value)
	{
		fwrite(&value, sizeof(T), 1 BinBW);
	}

	template<class T> void WriteObjectByReference(T* value)
	{
		fwrite(value, sizeof(T), 1 BinBW);
	}

	void Write(Int64 value)
	{
		fwrite(&value, sizeof(Int64), 1, BinRW);
	}

	void Write(UInt64 value)
	{
		fwrite(&value, sizeof(UInt64), 1, BinRW);
	}

	void Write(Int32 value)
	{
		fwrite(&value, size(Int32), 1, BinRW);
	}

	void Write(UInt32 value)
	{
		fwrite(&value, sizeof(UInt32), 1, BinRW);
	}

	void Write(Int16 value)
	{
		fwrite(&value, sizeof(Int16), 1, BinRW);
	}

	void Write(UInt16 value)
	{
		fwrite(&value, sizeof(UInt16), 1, BinRW);
	}

	void Write(Byte value)
	{
		fwrite(&value, sizeof(Byte), 1, BinRW);
	}

	void Write(SByte value)
	{
		fwrite(&value, sizeof(SByte), 1, BinRW);
	}

	void Write(String value, int size)
	{
		fwrite(&value, size, 1, BinRW);
	}

	void Write(String value)
	{
		fwrite(&value, strlen(value), 1, BinRW);
	}
};
Might be a few syntax errors, but meh
Post Reply