I'm trying to be a safe programmer, controlling extraction to guarantee no buffer overruns. However, if the data (text) in the stream has no white space that allows breaking at width(), it seems to ignore the width setting. Below is a fragment illustrating how I'm trying to use the operator:
1 2 3 4 5 6 7
ifstream TestWords ( TestWordFilename );
char TestWord[6] = {'\0'};
TestWords.width(5);
TestWords >> TestWord;
TestWords.ignore(INT_MAX, '\n'); // discard the remainder of the line
The above successfully extracts the first five letters from the file, but subsequent passes through the loop return a null string. I tried explicitly including the delimiter in the getline() method and also bringing back the ignore() method; neither made any difference.
What am I missing?
Also, as I read the description, width() should have done the job; what did I misunderstand?