My query is as follows. I have a file that is 16384 bytes in size. However i would like to break this down into 512 chunks of 32 bytes each. I am familaiar with straight forward loops but is there a way to initialise a loop to run for the entire 16384 bytes but somehow run for 32 bytes at a time if that makes sense


thanks

read 32 bytes at a time until end-of-file

char iobuf[32];
ifstream in("filename");
while( in.read(iobuf,sizeof(iobuf))
{
   // write it out
}

first thanks Ancient Dragon, u seem to come to the rescue loads lol.

would this be possible for example say i had a file of 10,000 bytes and the 16384 bytes i was interested in were somewhere in the middle. In my specific case i would not be able to read until EOF as bytes im interested in are floating around in the middle of the file.

Could the while loop be intialised to read for say only 16384 bytes instead of until EOF or would it have to be changed to a for loop?

call ifstream.seekg() to move the file position to there you want to start, then read until eof or until 16384 bytes have been read. all you need is another counter to count the number of bytes already read and stop when that is >= 16384.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.