You might also consider using C++ streams.
#include
#include
// open the file and create a file input stream
ifstream file("test.txt" , ios::in | ios::binary);
// loop through the whole file
while (ifs.good())
{
// extract one byte as the field width
unsigned char width;
file.read(&width, 1);
// extract width number of unformatted bytes
char * bytes = new char[width];
file.read(bytes, width);
// process the bytes
...
delete [] bytes;
// skip EOL characters if needed
// file.seekg(1, ios_base::cur)
}
file.close();
A simpler way if the newlines are included as you seem to indicate, would be to use getLine(). Check out http://www.cplusplus.com/reference/iostream/ifstream/ for more ways to use read(), get(), getLine() and lots of other great stream functions.