I am working to manually parse a binary database file. The data is stored in fixed width, fixed length flat files where integers and dates are stored using little endian binary values. There are several types I am dealing with which have anywhere from a single byte for an integer to 7 bytes for a timestamp.
I am new to C++ and trying to recreate a working system I did in C#, but there are some things that are tripping me up here. So the first thing I am doing is just reading 300 bytes from a file into a wchar_t
array.
int LineLength =300;
int counter = 0;
wint_t c;
wchar_t* wcs = new wchar_t[LineLength];
while ((c = fgetwc("BinaryFile.bin")) != WEOF) {
wcs[counter] = c;
counter++;
if (counter > LineLength - 1) {
break;
}
}
That seems to be working just fine. Now next I need to create a dynamic array size since some data like an int will be 1 byte, some will be strings and much longer. Normally I would set all ColumWidth
, StartingPosition
and such from looping over a JSON file, but for simplicity I just did them as arrays here:
string* ColumnName = new string[3]{ "Int1","Int2","String1" };
int* StartingPosition =new int[3] { 1,10,11 };
int* ColumnWidth =new int[3] { 4,1,25 };
for (int i = 0; 3 >= i; ++i)
{
wchar_t* CurrentColumnBytes = new wchar_t[ColumnWidth[i]];
cout << "\nBytes Requested: " << ColumnWidth[i]<< ", ArraySize: " << sizeof(CurrentColumnBytes) << "\n";
int Counter = 0;
for (int C = StartingPosition[i]; C <= ColumnWidth[i] + StartingPosition[i]; ++C)
{
CurrentColumnBytes[Counter] = wcs[C];
Counter++;
}
}
For some reason CurrentColumnBytes always gets a size of 4. I simplified things a little to:
int arraySize = 8;
char* locale = setlocale(LC_ALL, "UTF-8");
char* CurrentColumnBytes=new char[arraySize];
cout << "\nBytes Requested: " << arraySize << ", ArraySize: " << sizeof(CurrentColumnBytes) << "\n";
This prints out: Bytes Requested: 8, ArraySize: 4
. Changing it a little to be:
char* locale = setlocale(LC_ALL, "UTF-8");
char* CurrentColumnBytes[8];
cout << "ArraySize: " << sizeof(CurrentColumnBytes) << "\n";
This prints out: ArraySize: 32
There seems to be something basic I am missing here. Why can't I create an array with the width that I want? Why does it in the last example multiply it by 4 bytes?
Is there a better way to duplicate what I did using C# like this:
FileStream WorkingFile = new FileStream(fileName, FileMode.Open, FileAccess.Read);
while (WorkingFile.Position <= WorkingFile.Length)
{
byte[] ByteArray = new byte[300];
WorkingFile.Read(ByteArray, 0, 300);
}
I also tried using ifstream
, but I think there is a fundamental misunderstanding of creating arrays I am dealing with. I am including all the rest of the information in case someone has a suggestion for a better datatype or method.