473,809 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I need help

I have this code:

int RESULT_OF_BLACK LIST = 0;
int BlackListMethod (
{
FILE *blacklist;
char String[] = "I.am.a.dotted. string";
char Word[100];
if ((blacklist = fopen("blacklis t.dat", "r")) == NULL)
{
printf ("\n File could not be opened\n\n");
}
else
{
printf("%s\n", String);
fscanf(blacklis t,"%s", Word);
while (!feof(blacklis t))
{
fscanf (blacklist, "%s", Word);
if (strcmp(String, Word) == 0)
{
RESULT_OF_SCAN = 1;
}
else
{}
}
fclose(blacklis t);
}

}

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 1934
fe************* *@tiscali.it wrote:
I have this code:

int RESULT_OF_BLACK LIST = 0;
int BlackListMethod (
{
FILE *blacklist;
char String[] = "I.am.a.dotted. string";
char Word[100];
if ((blacklist = fopen("blacklis t.dat", "r")) == NULL)
{
printf ("\n File could not be opened\n\n");
}
else
{
printf("%s\n", String);
fscanf(blacklis t,"%s", Word);
while (!feof(blacklis t))
{
fscanf (blacklist, "%s", Word);
if (strcmp(String, Word) == 0)
{
RESULT_OF_SCAN = 1;
}
else
{}
}
fclose(blacklis t);
}

}

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_BLACK LIST. (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_BLACK LIST = 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("blacklis t.dat", "r")) == NULL)
{
printf ("\n File could not be opened\n\n");
}
else
{
printf("%s\n", String);
fscanf(blacklis t,"%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(blacklis t))
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_BLACK LIST.

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(blacklis t);
}

}
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_Keit h) 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.or g>
wrote:
fe************* *@tiscali.it writes:
char Word[100];
fscanf(blacklis t,"%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.ne t
Jul 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6334
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, column 13: there is no attribute "SRC" <bgsound src="C:\My Documents\zingwent.mids"> You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is...
5
2198
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: Objectives The purpose of this assignment is to have you practice the design of object-oriented classes, including one or more of the following concepts
0
1842
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, all dont work "Re:" need help "Re:need help"
9
2942
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 a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the device
7
3312
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
15
4651
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 communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
16
2546
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 uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
8
2756
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 the sequence nos and it should be such that i can access it through the structure .plz help me .
0
3969
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 Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
20
4314
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 is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.