How to obtain a wxUint8 array from a wxFile? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

How to obtain a wxUint8 array from a wxFile?

Post by Sunsawe » Wed Apr 04, 2007 9:29 pm

Hi,

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

Thanks

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Wed Apr 04, 2007 9:53 pm

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

Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe » Fri Apr 06, 2007 12:14 pm

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?

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Sat Apr 07, 2007 11:55 am

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

Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe » Sat Apr 07, 2007 1:28 pm

I think this should fit to my case.
But just to be sure, what do you mean by "not state-dependent"?

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Sat Apr 07, 2007 1:37 pm

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

eranif
Moderator
Moderator
Posts: 607
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif » Sat Apr 07, 2007 2:04 pm

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
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Sat Apr 07, 2007 2:43 pm

Your code will not work for big files (20-30 Mb). std::vector will keep allocating and reallocating until your computer will lag.

Joel

Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe » Sat Apr 07, 2007 2:54 pm

well...

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

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Sun Apr 08, 2007 8:12 am

I'm not sure, but I think it may be, because 1 raw byte takes ~1.66 bytes when base 64 encoded.

Joel

Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe » Mon Apr 09, 2007 12:44 am

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)

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Mon Apr 09, 2007 9:00 am

Read my comment. call strm.LastRead() to determine how much was read.

Joel

Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe » Mon Apr 09, 2007 1:46 pm

Oups...
Sorry... won't do it again...

Thanks for your help, it works perfectly!

Post Reply