I'm using C++ to read a file in chunks. The file contains integers, two per line.
First of all, I'm using this to find the file's length :
input.seekg (0, input.end);
int length = input.tellg();
input.seekg (0, input.beg);
After that, i check if length is bigger than chunksize, and if this is true, I allocate memory for the chunk...
char * buffer = new char [chunksize];
Ok, so here goes the reading function...
while (true)
{
input.read (buffer,chunksize);
cout<<buffer;
if(input.eof()) break;
}
Immediately after that I delete [] buffer;
However, I'm facing a problem with this code. For example, when the input file is like that :
2 5
4 5
6 8
7 5
4 2
1 2
The program will not output the expected characters, but something like :
2 5
4 5
6 8
7 5
4 2
1 2 2
1 2
Do you know the reason for these extra characters? If the file's size is less than chunksize, I input.read using its length and it works just fine. Maybe if using read with a length larger than the size of the file makes it not work correctly?
Thanks a lot