Guys,
Either there is a bug in the compiler or I'm getting dumber by the day. I want to read an entire file into memory. (Very small file, so no worries about size here). I followed the example here (from www.cplusplus.com)
// reading binary file
#include <iostream.h>
#include <fstream.h>
const char * filename = "example.txt";
int main ()
{
char * buffer;
long size;
ifstream file (filename, ios::in|ios::binary|ios::ate);
size = file.tellg();
file.seekg (0, ios::beg);
buffer = new char [size];
file.read (buffer, size);
file.close();
cout << "the complete file is in a buffer";
delete[] buffer;
return 0;
}
<< moderator edit: added [code][/code] tags and reformatted >>
However, when I use this code, I always get the first character of the file twice!!! For example, if I have a file which contains:
SAM LOVES TO TEST FILES
I end up with:
SSAM LOVES TO TEST FILES
Is this a known bug or am I not supposed to believe this reputable site? Is there a 'C' way to do this which is better?
Thanks,
Winbatch