Discussion:
questions about errors
(too old to reply)
Tsang Mathew
2004-02-05 16:35:27 UTC
Permalink
I had a few questions, should we be able to look for
1) double '-' signs,
2)double spaces?
3)repeated numberss in either + or - form
4)extra clause lines or not enough clauses
5)numbers after a terminating 0 on a line.
6)letters
7) a space after the terminating 0

THanks
Alexander Smith
2004-02-05 23:09:33 UTC
Permalink
(Scroll down.)
Post by Tsang Mathew
I had a few questions, should we be able to look for
1) double '-' signs,
This would be an error. If you're using scanf() to read the file, this is
an easy error to find, because "--5" is not a valid representation of an
integer.
Post by Tsang Mathew
2)double spaces?
No. Wherever you see a space in the files we've shown you, you should
consider it ok to have one or more whitespace characters. There may also
be whitespace at the start or end of each line.
Post by Tsang Mathew
3)repeated numberss in either + or - form
See my post "Re: Repetition in Literals" on Feb 2nd. Although it is
wasteful to repeat literals, it is not harmful, and to keep things
simple, we are not going to worry about it.
Post by Tsang Mathew
4)extra clause lines or not enough clauses
You should worry about not enough clauses. It would be good to worry about
too many clauses, but it can be a little tricky to get the code to work
correctly if, for example, you have a bunch of extra blank lines at the
end of the file. So, do consider the first, but you don't have to consider
the last if you don't want to.
Post by Tsang Mathew
5)numbers after a terminating 0 on a line.
We will not test this case. The reason is that there is an alternate
interpretation of the file specification which makes readCNF() easier
which makes this "error" not applicable: Instead of treating the file as
consisting of a number of lines, you can think of it as just a string of
integers separated by whitespace. Remember, '\n' is just another
whitespace character. In this case, you can just keep reading integers
with scanf("%d"...) and not worrying about lines. Whenever you read a 0,
you know you've reached the end of a clause.

If that's not the way you've written your readCNF() function, that's ok.
All of the files we give you will have 0 as the last number on any line
(except for the first line).
Post by Tsang Mathew
6)letters
Yes, definitely. After "p cnf", everything else should be integers.
Post by Tsang Mathew
7) a space after the terminating 0
No - see #2.
Post by Tsang Mathew
THanks
Alexander
Sheena Lynch
2004-02-06 01:42:22 UTC
Permalink
When is there too little clauses?
Is it when numClauses is greater than the # of clause info lines in the
file, or the other way around?
Post by Alexander Smith
Post by Tsang Mathew
4)extra clause lines or not enough clauses
You should worry about not enough clauses. It would be good to worry about
too many clauses, but it can be a little tricky to get the code to work
correctly if, for example, you have a bunch of extra blank lines at the
end of the file. So, do consider the first, but you don't have to consider
the last if you don't want to.
Alexander Smith
2004-02-06 02:51:42 UTC
Permalink
There are too few clauses when numClauses is greater than the number of
clauses in the file. For example, if numClauses is 5, and you only have
four 0's in the file, then there are too few clauses. This is the case you
should catch.

If there are too many clauses (i.e. extra lines in the .cnf file), this is
a little harder to catch. So, for this particular case for this lab, we're
not going to check that. You have enough other cases to worry about. (If
you have a burning desire to put in code to check this case, you can go
ahead and do it, but it won't be checked by the test cases you will be
marked with.)

Alexander
Post by Sheena Lynch
When is there too little clauses?
Is it when numClauses is greater than the # of clause info lines in the
file, or the other way around?
Post by Alexander Smith
Post by Tsang Mathew
4)extra clause lines or not enough clauses
You should worry about not enough clauses. It would be good to worry about
too many clauses, but it can be a little tricky to get the code to work
correctly if, for example, you have a bunch of extra blank lines at the
end of the file. So, do consider the first, but you don't have to consider
the last if you don't want to.
Chris Brelski
2004-02-06 02:27:45 UTC
Permalink
Post by Alexander Smith
Post by Tsang Mathew
6)letters
Yes, 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
possibilities:
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?
Alexander Smith
2004-02-06 03:13:07 UTC
Permalink
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 Brelski
Post by Alexander Smith
Post by Tsang Mathew
6)letters
Yes, 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?
Chris Brelski
2004-02-06 14:55:21 UTC
Permalink
so just to confirm : in general
fscanf(" %<var type> " , <var_type_variabe>) returns n if n correct type
variables have been scanned?
Post by Alexander Smith
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.
Alexander Smith
2004-02-06 16:33:53 UTC
Permalink
Yes, that is correct. (Except that you usually need & before
<var_type_variable> except for strings.)

If it can't match all of the fields, it will tell you how many it did
match. The input stream will have stopped at the first character it
couldn't match. (So, theoretically, you could do some kind of correction
or error processing, and then continue from where you left off.) For a
complete, detailed description of the scanf functions, see the man pages.
(Run "man fscanf"; space and w go one page forward/backwards.) The man
pages actually provide a thorough reference for all of the standard C
libraries, although they can sometimes be a little heavy to read through.

Alexander
Post by Chris Brelski
so just to confirm : in general
fscanf(" %<var type> " , <var_type_variabe>) returns n if n correct type
variables have been scanned?
Post by Alexander Smith
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.
Sherman
2004-02-06 09:49:15 UTC
Permalink
Regarding the case with too few clauses, isnt that equivalent to the case
before where if a clause is NULL (empty) then it returns SAT? Since all
clauses are initialized as NULL, if there were too few clauses to be read
from the .cnf file then the remaining clause(s) would remain NULL (empty).
Please verify.
Thank you.

Sherman
Alexander Smith
2004-02-06 14:10:32 UTC
Permalink
No, if the first line in the .cnf file says there are N clauses, then what
it's saying is "this file contains N clauses". So, if the file appears to
contain fewer than N clauses, then the file has been corrupted. If you
wanted to put empty clauses in the file, you would need to do something
like this:

p cnf 3 3
1 2 0
0
3 -1 2 0

Alexander
Post by Sherman
Regarding the case with too few clauses, isnt that equivalent to the case
before where if a clause is NULL (empty) then it returns SAT? Since all
clauses are initialized as NULL, if there were too few clauses to be read
from the .cnf file then the remaining clause(s) would remain NULL (empty).
Please verify.
Thank you.
Sherman
Loading...