ifstream extraction limiting

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 


What obvious thing am I overlooking?

Lance ==)-------------
Try getline method instead.

 
TestWords.getline(TestWord,6);
Thanks for the suggestion. Unfortunately, it made things worse.

1
2
3
4
5
6
7
8
ifstream TestWords ( TestWordFilename );
char TestWord[6] = {'\0'};

while ( !TestWords.eof() ) {
  TestWords.getline( TestWord, 6 );
  cout << "'" <<  TestWord << "'" << endl;
  TestWord[0]  = '\0';
  }


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?

Lance ==)-----------------
Try below see how.

1
2
3
while ( TestWords.getline( TestWord, 6 ) ) {  
  cout << "'" <<  TestWord << "'" << endl;
  }
Topic archived. No new replies allowed.