Before trying to write code to read this file, you should think a little more about how the file is defined -- precisely how it's defined.
Informally, the definition of the file is "the first column is a string possibly containing whitespace, and the second column is an integer". But what separates the columns?
If the columns are separated by whitespace, and if the first column can contain whitespace, then the first column isn't really the first column, it's potentially multiple columns. That is, the line
Coca Cola 100
really contains three columns.
So if we want to go down this road, we have to try to differentiate between a second column that's an integer, and a first column that (though it might contain whitespace) does not look like an integer.
But if we go down that road, we have two pretty significant problems:
It's hard to code. It's probably impossible to code satisfactorily using scanf
or sscanf
alone.
It's still ambiguous. What if Coca Cola comes out with a new product "Coca Cola 2020"? Then we'll have a line like
Coca Cola 2020 50
So my bottom line is, if it was me, I wouldn't even try to write code to parse this file format. I would come up with a cleaner, less ambiguous file format, perhaps
Coca Cola, 100
or
"Coca Cola",100
or
Coca Cola|100
and then write some clean and simple code to parse that. (I probably still wouldn't use scanf
, though; I'd probably use something more like strtok
. See also this chapter in my C Programming notes.)
Addendum: the other road to potentially go down is to count columns from the right-hand edge. In this case, you could write code to, in effect, say that the product name is in columns 1 to N-1, and the count is column N. This can work as long as there's at most one "column" containing whitespace.