file copying in c++

V

vasanth kumar

Hi,

I have a file 31oct2003.xls. I want to make a copy of this file with a new name, say:2nov2003.xls. But my program is crashing at hte line
char* buffer = new char[size];
saying could not allocate memory.

Any solution to this problem?

---------------------
ifstream oldfile(31oct2003.xls, ios::in|ios::binary|ios::ate);
long size = oldfile.tellg();
char* buffer = new char[size];
oldfile.seekg (0, ios::beg);
oldfile.read(buffer,size);

ofstream weekrep(stime_1, ios::binary||ios::eek:ut);
weekrep.write(buffer,size);
 
O

.oO LGV Oo.

"vasanth kumar" <[email protected]> a écrit dans le message de Hi,

I have a file 31oct2003.xls. I want to make a copy of this file with a new name, say:2nov2003.xls. But my program is crashing at hte line
char* buffer = new char[size];
saying could not allocate memory.

Any solution to this problem?

---------------------
ifstream oldfile(31oct2003.xls, ios::in|ios::binary|ios::ate);
long size = oldfile.tellg();
char* buffer = new char[size];
oldfile.seekg (0, ios::beg);
oldfile.read(buffer,size);

ofstream weekrep(stime_1, ios::binary||ios::eek:ut);
weekrep.write(buffer,size);
 
J

Jacek Dziedzic

vasanth kumar said:
Hi,
I have a file 31oct2003.xls. I want to make a copy of this file
with a new name, say:2nov2003.xls. But my program is
crashing at hte line char* buffer = new char[size];
saying could not allocate memory.

Check what size is before the new[].

HTH,
- J.

PS. Do not, under any circumstances, post in HTML to a ng,
especially this one or you'll get flamed.
 
M

Mike Wahler

I might be wrong, but I'd say that the value of size is zero... Chech
this value first.


new char[0] is valid (although obviously not what OP wants).

I suspect OP is simply requesting more memory than the
host environment can provide.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top