Re: I need help



federico_bertola@xxxxxxxxxx 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) kst-u@xxxxxxx <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.
.



Relevant Pages

  • Re: fscanf
    ... int main ... The the rv that catches the return value of fscanf reported 0. ... And that leaves the "string" specification as being the dodgy bit. ... Try some other specifications first. ...
    (comp.lang.c)
  • Re: Shorthand conditional doesnt evaluate right
    ... Operator '>' cannot be applied to operands of type 'string' and 'int' ... Isn't Length an int? ... I don't think that's your real code. ...
    (microsoft.public.dotnet.general)
  • Re: how to change decimal symbol in VB / Access?
    ... PS> that's right TIM, some of the code wasn't real, becouse the real code is ... > string and double has a lower case letters, so I assume that this is not ... the backslash forces the use of a dot rather than whatever OLEAUT32 ...
    (microsoft.public.access.modulesdaovba)
  • Re: Accessing Bytsavailable in Inputbuffer
    ... there is no flag to pass to fscanf to indicate ... would be interpreted as a format specifier. ... a line terminator was reached or a character that did not match 's' ... the cell string array, whereas reading with the default '%c' ...
    (comp.soft-sys.matlab)
  • Re: Problem with xsl to make a text control disabled
    ... It would help if you gave us a stand-alone runnable example that provokes the problem, rather than a fragment. ... maybe comparing it with your real code will help you figure out what you're doing wrong. ... Second case: The string has been set to [<xsl:value-of ...
    (comp.text.xml)