Can I use scanf to require that a certain string appears next in the input?
<p>I want to parse a file format that looks like this:</p>

<pre class="lang-none prettyprint-override"><code>SECTION Foo
10
END
</code></pre>

<p>I tried using the following code to do the parsing. It should return 1 in case of a parsing error:</p>

<pre><code>int main()
{
    char str[100];
    int n;

    int nr1 = scanf("SECTION %s", str);
    if (nr1 != 1) return 1;

    int nr2 = scanf("%d", &amp;n);
    if (nr2 != 1) return 1;

    int nr3 = scanf("END");
    if (nr3 != 0) return 1;

    printf("OK! %s %d\n", str, n);
    return 0;
}
</code></pre>

<p>However, the <code>OK!</code> message shows up even before I type <code>END</code> in the standard input. What I actually want is to detect if the next string that appears in stdin is <code>END</code> and return with an error code otherwise. Is there a way that I can modify the <code>scanf("END")</code> line to achieve this?</p>

<p>Right now, the only way I can think is to use a <code>%s</code> scanf followed by a <code>strcmp</code> but it seems inelegant to have to introduce extra temporary variables just for this.</p>