Discussion:
fscanf problem
(too old to reply)
Stupar Andrija
2004-02-06 01:41:16 UTC
Permalink
I have a little problem using fscanf. Now the cnf files all begin with "p
cnf". I declare a string char fileheader[6]; and then use the following
command when reading the file:

fscanf(fs,"%s %d %d", fileheader, &numvariables, &numclauses);

Now this always gives me just "cnf" rather than "p cnf". I've tried
reading in a character (or 2) before the string, but I still just get
"cnf" that, is the "c" goes into the character while the string becomes
"nf". Does anyone know what is going on here?

Thanks
Alexander Smith
2004-02-06 02:55:31 UTC
Permalink
If you do fscanf(fs, "%s %d %d", fileheader, &numvariables, &numclauses);,
you should get "p" in fileheader, and fscanf will fail at the first %d.
With the scanf funcitons, "%s" scans one string, separated by whitespace.
The "p" has whitespace after it. So, you would need to do something like
fscanf(fs, "%s %s %d %d"...). But that's still not the best way to do it.
You don't need to store the "p cnf" anywhere, so why bother? Just do this:

fscanf(fs, "p cnf %d %d", &numvariables, &numclauses);

If you have text in the scanf funcitons (i.e. text not preceeded by %),
the scanf function will match that text again the input data and skip over
it. (Note: whitespace in the format string is treated a little
differently, as I described in a post somewhere way back for lab 1.)

Alexander
Post by Stupar Andrija
I have a little problem using fscanf. Now the cnf files all begin with "p
cnf". I declare a string char fileheader[6]; and then use the following
fscanf(fs,"%s %d %d", fileheader, &numvariables, &numclauses);
Now this always gives me just "cnf" rather than "p cnf". I've tried
reading in a character (or 2) before the string, but I still just get
"cnf" that, is the "c" goes into the character while the string becomes
"nf". Does anyone know what is going on here?
Thanks
Loading...