472,986 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

I need help

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
3 1866
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.