By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

I need help

P: n/a
I have this code:

int RESULT_OF_BLACKLIST = 0;
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);
while (!feof(blacklist))
{
fscanf (blacklist, "%s", Word);
if (strcmp(String,Word) == 0)
{
RESULT_OF_SCAN = 1;
}
else
{}
}
fclose(blacklist);
}

}

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.
Why?
Thx in advance!

Jul 4 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
fe**************@tiscali.it wrote:
I have this code:

int RESULT_OF_BLACKLIST = 0;
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);
while (!feof(blacklist))
{
fscanf (blacklist, "%s", Word);
if (strcmp(String,Word) == 0)
{
RESULT_OF_SCAN = 1;
}
else
{}
}
fclose(blacklist);
}

}

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.
What do you mean by "all works?" That is, how does the
result you actually get differ from what you expected?

Here's our difficulty: You have not explained what you
intend this function to do. You have told us that the code
you've shown does not do what you want, but that leaves us
a large set of possible behaviors ("everything this code
doesn't do") that might be what you're looking for. How are
we to guess, given such a large universe of possibilities?

Still, I can point out a few suspicious things:

1: The code as shown will not compile. This makes me suspect
that when you wrote "I have this code" you were lying: You
have some code that looks something like "this code," but you
are keeping your actual code a secret. We can debug "this
code" all day long, but that may not be of any help with your
actual problem.

In what follows, I'll assume "this code" has been repaired by
adding #include <stdio.hand #include <string.h>, by adding `void)'
to what is now the second line, and by changing RESULT_OF_SCAN to
RESULT_OF_BLACKLIST. (If these repairs do not produce your actual
code, you have no one but yourself to blame.)

2: The BlackListMethod() function returns an `int' value --
except that it doesn't return anything at all. There is no
way to tell what will happen if another function calls
BlackListMethod() and tries to use the value that the latter
fails to return.

3: The function ignores the first word of the file, unless it
also happens to be the final word (see below).

4: The function processes the final word of the file twice,
unless it also happens to be the first word (see above).

5: The "%s" specifier used with scanf() or fscanf() is a recipe
for trouble, since it suffers from all the same problems that
gets() has. See the FAQ.

Federico, there are people on this newsgroup who are willing
to help you. But you are making things difficult for them by
failing to describe your problem adequately and by refusing to
reveal the code that is causing it. If you break your arm, do you
tell the doctor you have a stomachache? If the doctor treats you
for the imaginary stomachache, will the treatment heal your arm?

Learn to describe your problem accurately and thoroughly: tell
us what you want the code to do, what it actually does, and why
you are not pleased with its behavior. And then, for goodness'
sake, show the actual code and not a half-baked paraphrase!

--
Eric Sosman
es*****@acm-dot-org.invalid
Jul 4 '06 #2

P: n/a
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.
Jul 4 '06 #3

P: n/a
On Tue, 04 Jul 2006 19:48:08 GMT, Keith Thompson <ks***@mib.org>
wrote:
fe**************@tiscali.it writes:
char Word[100];
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.
And a null terminator byte is added.
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 mean %99s. Or, perhaps more convenient it you want to use a macro
to replace the magic numbers, use something more or less like:
char Word [100+1]; ... fscanf ( , "%100s", Word);
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).
Yes and almost: it returns the number of items matched _and stored_,
or EOF. This differs if you use %*blah 'suppressed' items.

<snip some>
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.
Right. If you really want to parse input at dots using *scanf, use a
format specifier like %99[^.] .

Or better, don't use *scanf at all. Read the line into a buffer and
break it up yourself with strchr() or explicit code; or strtok() if
you don't need to preserve the original, recognize 'null' components,
allow for calls to or from other code using it, or multithreading.
- David.Thompson1 at worldnet.att.net
Jul 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.