fe**************@tiscali.it writes:
I have this code:
int RESULT_OF_BLACKLIST = 0;
All-caps is normally used for macros, not for variable names.
int BlackListMethod(
{
FILE *blacklist;
char String[] = "I.am.a.dotted.string";
char Word[100];
if ((blacklist = fopen("blacklist.dat", "r")) == NULL)
{
printf ("\n File could not be opened\n\n");
}
else
{
printf("%s\n", String);
fscanf(blacklist,"%s", Word);
fscanf() with the "%s" option reads a white-space delimited string.
Specifically:
Input white-space characters are skipped (as specified by isspace()).
A sequence of non-white-space characters is matched and copied to
String.
Also, scanf with "%s" is potentially dangerous; it will read and copy
and arbitrarily long sequence of characters, possibly overflowing
Word. You can use "%100s" to avoid this problem.
You must check the result of the fscanf() function. It returns the
number of items matched (in this case it could be 0 if nothing in the
input matches), or EOF on an error or end-of-file (see below).
while (!feof(blacklist))
This is a misuse of the feof() function. feof() can be used *after*
you've reached the end of your input to determine whether it was the
result of an end-of-file condition or an error condition. Any other
use is probably incorrect.
The fscanf function will tell you whether it reached end-of-file by
returning the value EOF. Use that.
Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
Question 12.12 is particularly relevant, but read all of it.
{
fscanf (blacklist, "%s", Word);
if (strcmp(String,Word) == 0)
{
RESULT_OF_SCAN = 1;
You haven't declared RESULT_OF_SCAN. You *have* declared something
called RESULT_OF_BLACKLIST.
This isn't your real code, is it? If you want us to help you debug
your code you need to show it to us. If you paraphrase it, as you've
done here, we can't possibly guess whether any problems in the code
you posted have anything to do with any problems in the actual code
you're compiling and running.
Post real code. Don't re-type it, copy-and-paste it.
}
else
{}
There's no need for an empty else clause.
}
fclose(blacklist);
}
}
Your function is declared to return an int, but you don't return
anything. If you want to return an int, return an int. If not,
declare the function to return void (i.e., not to return anything).
>
The blacklist.dat file contain a sample dotted string like
"I.am.a.dotted.string",
now, all works if 'String' and 'Word' are "normal" string but they
don't match if they are dotted.
I *think* you're expecting "%s" to match the "I", "am", "dotted", and
"string" substrings. It doesn't; it matches based on whitespace.
But this is only a guess since, as I mentioned, you haven't shown us
your real code.
Fix the problems I've indicated here and try again. If you're still
having problems, post a small self-contained compilable program that
we can try ourselves.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.