I was making a program that read binary files. So, I read the individual bytes into unsigned chars (actually reading the data as chars and casting them to unsigned chars for each character). Now I have to write back the unsigned chars to the binary file.
The problem is that now I am forced to write individual bytes after casting them to chars (because write() for binary files expects char* buffer). So, now i have to do the following:
for(int x=0; x<data_size; x++)
{
ch=(char)data[x];
outfile.write(&ch,1);
}
Is there any way to get around this thing so that the amount of I/O operations are reduced in case of reading and writing?
You can do the casting on a pointer…
the same can be done for reading (i.e. just pass a pointer to the first element of an array of unsigned char casting it to a pointer to char).