I'm using Borland's C++ 5.5 compiler with Code::blocks.I was experimenting with file handling and used the seekg() function to move the get pointer , the program seems to compile without any errors but when I run it , it crashes.The program runs fine if I don't use seekg().Also, seekp() function works fine and the program doesn't crash.. but it doesn't seem to work.I'm using code::blocks version 10.05.I copied a program from a c++ tutorial site which demonstrates the use of seekg(), it still crashes.The exact line I typed is "fin.seekg(0,ios::beg)", is it wrong?Any help?
Thanks.
If I delete "file.seekg (0, ios::beg);", it works.Code:#include <iostream.h>
#include <fstream.h>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("example.bin", ios::binary);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "the complete file content is in memory";
delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}