I think with my last post I have already said more than I should have
about error checking. I will give a few more general guidelines here, and
then that's it. First of all, you generally need to do most of your error
checking on data that comes from the "outside world" - like user input or
files - because users make mistakes. Once you know the data in your
program is correct, it will generally stay correct unless you have bugs in
your code. So, other error checking you do in your program, while often
not strictly necessary, is good "defensive programming" practice, because
it will help you find bugs in your own code while you are working on it.
When you are error checking the .cnf files in readCNF(), consider this
question: "What could someone stick in the .cnf file that would cause my
program to fail?". If the program was just characters or was empty, could
you read it as a boolean formula? If it contained a control character,
would that make sense as part of a clause? If a literal was 3.14, could
you add it to a clause? If the answer to any of these questions is "no",
then you should do something about it. Note: That does NOT mean you have
to write a special case for every one of the questions that you have asked
below or that have been asked earlier. You can take care of most of these
cases very easily by using the return value of fscanf() intelligently. All
of the *scanf() function return the number of fields they successfully
scanned. If this does not equal the number of % signs in your input string
(except for %% which is a special case), then there was an error. To see
examples of how to use this, look numint_main.c from lab 1.
The labs in this class involve doing some programming. Part of programming
involves thinking about and handling errors. If I tell you exactly which
errors can and can't happen, then I am doing part of your lab for you, and
I've already done more of that than I probably should have. The rest is up
to you.
Alexander
Post by Chris BrelskiPost by Alexander SmithYes, definitely. After "p cnf", everything else should be integers.
is this implying that we should handle files that begin with something
like "p cnf 4 5 abcd", "p cnf abcd 4 5", "p cnf " and others?
with the line fscanf(fs, "p cnf %d %d", &var1, &var2) these files cause
the program to hang. This condition is very problematic and implies other
What if the file is just characters?
What if the file contains nothing?
What if it contains control characters?
What if the literals are "3.14" or "2.71828" or other strange data? this
seems just as unlikely as "--5"...
is all of this really necessary?