Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how save binary file into variable ??

0 views
Skip to first unread message

agus tegar

unread,
Jun 9, 2009, 8:05:30 AM6/9/09
to
please help me how import / save binary file into variable ( byte
array or ect )??


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Thomas Matthews

unread,
Jul 1, 2009, 10:19:15 PM7/1/09
to
agus tegar wrote:
> please help me how import / save binary file into variable ( byte
> array or ect )??
>
>
Very simple.
For starters, please study the following concepts:
std::ostream.write
std::istream.read
new

1. Allocate a big contiguous area in memory, large enough
to contain the contents of the file.
For example:
const unsigned int MAX_SIZE = 10 * 1024 * 1024;
unsigned char * p_big_file(0);
p_big_file = new unsigned char [MAX_SIZE];
if (!p_big_file)
{
// memory allocation failure,
// process it here.
}

2. Use istream.read to input the file:
ifstream the_data_file("filename", ios::binary);
// It may need to be ios_base::binary
the_data_file.read(p_big_file, MAX_SIZE);

I'll leave obtaining the number of bytes read as
an exercise for the OP.

Some notes:
1. Many platforms cannot allocate huge amounts of memory
as local variables (a.k.a. stack variables), or even
as global variables.
2. Many platforms have more dynamic memory space than
the other types. This is commonly known as the heap.
3. Your platform, compiler, or OS may have functions to
treat files as memory. Look it up.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Krzysiek Czaiński "Czajnik"

unread,
Jul 3, 2009, 4:09:27 PM7/3/09
to
On 2 Lip, 04:19, Thomas Matthews

<Thomas_Really_Hates_S...@matthews.cox.net> wrote:
> const unsigned int MAX_SIZE = 10 * 1024 * 1024;
> unsigned char * p_big_file(0);
> p_big_file = new unsigned char [MAX_SIZE];
> if (!p_big_file)
> {
> // memory allocation failure,
> // process it here.
> }

Correct me if I'm wrong: new unsigned char [MAX_SIZE] will throw
std::bad_alloc if it fails to allocate memory, and it will never
return 0.

The code should therefore look like this:
try {


const unsigned int MAX_SIZE = 10 * 1024 * 1024;

unsigned char * p_big_file = new unsigned char [MAX_SIZE];
// do somthing with p_bif_file
}
catch ( std::bad_alloc& )


{
// memory allocation failure,
// process it here.
}

Regards

--

Steve Donnelly

unread,
Jul 4, 2009, 8:55:30 AM7/4/09
to
On Jun 9, 8:05 am, agus tegar <agoestafin.gr...@gmail.com> wrote:
> please help me how import / save binary file into variable ( byte
> array or ect )??

If you don't know the size of the file beforehand, you can determine
the length at runtime and allocate an array that fits the file
exactly.

The following example was taken from:
http://cplusplus.com/reference/iostream/istream/seekg/

// load a file into memory
#include <iostream>
#include <fstream>
using namespace std;

int main () {
int length;
char * buffer;

ifstream is;
is.open ("test.txt", ios::binary );

// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);

// allocate memory:
buffer = new char [length];

// read data as a block:
is.read (buffer,length);

is.close();

cout.write (buffer,length);

delete[] buffer;
return 0;

Martin T.

unread,
Jul 6, 2009, 10:34:42 AM7/6/09
to
Krzysiek Czaiński Czajnik wrote:
> On 2 Lip, 04:19, Thomas Matthews
> <Thomas_Really_Hates_S...@matthews.cox.net> wrote:
>> const unsigned int MAX_SIZE = 10 * 1024 * 1024;
>> unsigned char * p_big_file(0);
>> p_big_file = new unsigned char [MAX_SIZE];
>> if (!p_big_file)
>> {
>> // memory allocation failure,
>> // process it here.
>> }
>
> Correct me if I'm wrong: new unsigned char [MAX_SIZE] will throw
> std::bad_alloc if it fails to allocate memory, and it will never
> return 0.
>

Unless you use VC6 or specifically tell the compiler not to. (Maybe
because you have used VC6 and don't want the code to break in VC8.)

br,
Martin

0 new messages