Question on the limits include. What does this statement mean?

inventoryDataFile.ignore(numeric_limits<streamsize>::max(), '\n');

inventoryDataFile is an fstream object.


Can you explain to me in detail what this statement does? I've been using it after the recommendation and help of a user. But I'm stumbling upon a problem and I feel like I have to know what this means and how I'm using it in order to debug my program.

Any help is appreciated.

Thanks in advance,

Regards,

Hugo.
It tells the stream to skip over any newline characters at the top of the buffer, up to the maximum possible number of characters that the stream could ever buffer. std::streamsize is the type of the variable that holds the size of the stream buffer, and std::numeric_limits<T>::max() is the maximum numeric value that type T can hold.
It tells the stream to skip over any newline characters at the top of the buffer, up to the maximum possible number of characters that the stream could ever buffer. std::streamsize is the type of the variable that holds the size of the stream buffer, and std::numeric_limits<T>::max() is the maximum numeric value that type T can hold.


Do you mean when there is a '\n' at the end of the stream, it deletes it and stop the stream there?

Or does it ignore all '\n' for the following stream actions?

Would you mind giving an example? I don't think it's 100% clear as of now
Suppose you have a file that contains student grades and names:
82	Dave
93	Scottie
88	Bob
75	Ginger


Here is a first attempt of a program that reads the grades and prints them out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;

int
main()
{
    int grade;
    while (cin >> grade) {
	cout << grade << '\n';
	cin.ignore(30, '\n');	// ignore 30 characters or until you ignore a newline
    }
    return 0;
}

Line 13 extracts and discards characters from cin until it extracts 30 characters, or it extracts a '\n' character, or it reaches the end of the stream. Note that the '\n', if seen, will be extracted and discarded. So the next character you try to read is the one after the '\n'.

So far so good, when I run the program on the above input, I get this:
82
93
88
75


But what if the first name is really long?
82      Mr. David M Hayden of the great state of New Jersey.

Now when I run the program it just prints 82. Why? Because it ignored 30 characters without seeing a '\n'. The name is longer than 30 characters so it stopped there. The next time through the loop, it wasn't able to extract a grade number because it the next characters in the stream were letters.

What to do? We can increase to 30 to something bigger, line 100. But what's to prevent the name from being larger than 100 characters. How about 1000 characters? Surely no name will be longer than that. But what if some hacker decides to feed the program bad input and deliberately creates a file a "name" that's 1001 characters? Or 100,000 characters? Or 1,000,000 characters?

You want to ignore the most characters that could possibly be in the stream. And that number is the maximum value of type type streamsize. You can get this value portably using the numeric_limits template: numeric_limits<streamsize>::max().

Here is a reference for ignore():
http://www.cplusplus.com/reference/istream/istream/ignore/
Last edited on
For example, if you do
1
2
int n;
std::cin >> n;
and the user enters, for example, "42\n", the operator>>() call reads up to the newline and leaves it in the buffer. This is not a problem if the next time you read from std::cin you use operator>>(), but if you do something like
1
2
std::string line;
std::getline(std::cin, line);
std::getline() will read up to and including the first newline, which in this case is the first character in the buffer. This, to std::getline(), is indistinguishable from the user hitting the Enter key without typing any other input, so line ends up containing an empty string.
The ignore() call above is intended to address this issue.
Thanks a lot to you two! This made things much cleared even if it wasn't that complicated.

Your and effort is very much appreciated!

Regards,

Hugo.
Topic archived. No new replies allowed.