0

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

user2455103
  • 385
  • 2
  • 3
  • 12

1 Answers1

2

Your string is not NULL-terminated. The read() function will not put a '\0' at the end of what it reads for you, so when you go to print it, you're printing essentially garbage data beyond the end of what you read, because the print code is expecting a NULL terminator to mark the end of the string.

TypeIA
  • 15,869
  • 1
  • 32
  • 47
  • Thanks for your answer... Well, I tested what you are saying, by adding an '\0' at the buffer[length], but really, nothing happened. – user2455103 Jan 17 '14 at 14:33
  • @user2455103 Is `buffer` big enough to hold the extra `'\0'`? (It must be 1 byte larger than the amount you're reading.) Also, you are not checking the return value of `read()` to see how many bytes were actually read. Don't assume it's reading as much as you're asking for (especially if you're asking for more than the size of the file). – TypeIA Jan 17 '14 at 14:36
  • Oh yes... You are right!!! By using gcount(), and printing only the number of read chars, the output is clean and nice!!! +1 – user2455103 Jan 17 '14 at 14:38