Page 1 of 1

How to obtain a wxUint8 array from a wxFile?

Posted: Wed Apr 04, 2007 9:29 pm
by Sunsawe
Hi,

I'm looking for a way to have the content of a wxFile in a wxUint8 array.
Is it possible? How?

Thanks

Posted: Wed Apr 04, 2007 9:53 pm
by lowjoel

Code: Select all

wxFileInputStream strm(wxT("FileName"));
wxUint8* arr = new wxUint8[strm.GetLength()];
strm.Read(arr, strm.GetLength()); //arr has the contents of the entire file now
Try not to use this approach for large files (for obvious reasons such as your memory)

Joel

Posted: Fri Apr 06, 2007 12:14 pm
by Sunsawe
Thanks for that hint.

I can easily understand why it's not the best thing for large files but...
How to do in this case?
I load the file to encode it after so how can i split it without distubing the encoding process?

Posted: Sat Apr 07, 2007 11:55 am
by lowjoel
If your code is not state-dependent you can always read 65536 bytes at a time, using Eof to control if you should read more:

Code: Select all

wxFileInputStream strm(wxT("FileName"));
wxUint8 arr[65536];
while (!strm.Eof())
{
    strm.Read(arr, 65536); //arr has the last 65536 bytes (or less, depending of the file pointer's position)
    //do stuff with arr. The first strm.LastRead() bytes are valid.
}
Joel

Posted: Sat Apr 07, 2007 1:28 pm
by Sunsawe
I think this should fit to my case.
But just to be sure, what do you mean by "not state-dependent"?

Posted: Sat Apr 07, 2007 1:37 pm
by lowjoel
uhm... if it means that the current 65535 bytes does not depend on the 65535 bytes being read before the current one.

Like for example if you are using a CBC or EAX mode encrypted file reading the current block depends on the block before it read so the operation can succeed (like CBC is an XOR with the previous block IIRC), so you cannot jump to the middle of the file and start reading the contents and decrypting the file.

Joel

Posted: Sat Apr 07, 2007 2:04 pm
by eranif
I'd do it like this:

Code: Select all

wxFileInputStream input( "myfile.txt" );
wxTextInputStream text( input );
wxUint16 i;
std::vector<wxUint16> array;
while( !input.Eof() )
{
	text >> i;       // read a 16 bit integer.
	array.push_back(i);
}

Eran

Posted: Sat Apr 07, 2007 2:43 pm
by lowjoel
Your code will not work for big files (20-30 Mb). std::vector will keep allocating and reallocating until your computer will lag.

Joel

Posted: Sat Apr 07, 2007 2:54 pm
by Sunsawe
well...

Does somebody know if base64 encoding is "state-dependent"?

Posted: Sun Apr 08, 2007 8:12 am
by lowjoel
I'm not sure, but I think it may be, because 1 raw byte takes ~1.66 bytes when base 64 encoded.

Joel

Posted: Mon Apr 09, 2007 12:44 am
by Sunsawe
Hi,
I tried to use the piece of code that Joel gave.
lowjoel wrote:

Code: Select all

wxFileInputStream strm(wxT("FileName"));
wxUint8 arr[65536];
while (!strm.Eof())
{
    strm.Read(arr, 65536); //arr has the last 65536 bytes (or less, depending of the file pointer's position)
    //do stuff with arr. The first strm.LastRead() bytes are valid.
}
But a question came to my mind... in the case of the last bytes, less than 65536 bytes are probably going to be read. Then, how will i know exactly how many have been read?

Is the n+1 position of the array going to be a '\0'?
(which will stop the encoding so no matter with the reel size)

Posted: Mon Apr 09, 2007 9:00 am
by lowjoel
Read my comment. call strm.LastRead() to determine how much was read.

Joel

Posted: Mon Apr 09, 2007 1:46 pm
by Sunsawe
Oups...
Sorry... won't do it again...

Thanks for your help, it works perfectly!