473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 1893
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.