I want to read 8byte chunks from a binary file at a time till I reach end of the file. Why doesn't this code work? What are the alternatives?
// read a file into memory
#include <iostream>
#include <fstream>
using namespace std;
int main () {
long long int * buffer;
ifstream is;
is.open ("test.txt", ios::binary );
// allocate memory:
buffer = new long long int;
// read data:
while(!is.eof())
is.read (buffer,sizeof(long long int));
is.close();
delete[] buffer;
return 0;
}
If I replace all long long int
with char
, the code works perfectly.
Ref: code adapted from www.cplusplus.com
new
does not match withdelete []
.new[]/delete[]
operators with plain ones will compile, but will bring you into Undefined Behavior land, where anything is possible - the program might run fine, your hard-drive might blow up, or the mouse might eat your cat.