473,790 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R C and strings

I wonder if anyone has time to write a small example program based on
this data or to critique my own effort?

A file called Realm List.html contains the following data:
Bladefist-Horde
Nordrassil-Horde
Draenor-Alliance
Nordrassil-Alliance
Nordrassil-Neutral
Draenor-Horde
Moonglade-Horde
Shadowmoon-Horde
Moonglade-Alliance

I need an algorithm that takes from this list the names of Realms that
we have both Alliance and Horde data for and to list them in
alphabetical order. My desired format of this list will be:
Draenor-Alliance
Draenor-Horde
Moonglade-Alliance
Moonglade-Horde
Nordrassil-Alliance
Nordrassil-Horde

I'm committed to using ANSI C with the K&R book as my guide. It says a
string is an array of chars.

Right now I can open the file, I can use strtok to split each line but
I can't work out how to create an array of these strings?

FILE *realmList;
if((realmList=f open("Realm List.html","w+" )) == NULL)
{
printf("Couldn' t open \"Realm List.html\" for output\n");
return 1;
}

/*
List Realms and count of each
*/
char strLine[244];

while (fgets(strLine, 244, realmList))
{

char *tmp = NULL;
char token[] = "-";
tmp = strtok(strLine, token);

printf("%s\n", tmp);
/* stuck at this point */
}

Sep 24 '06
19 3116
Richard Heathfield wrote:
we******@gmail. com said:
.... snip ...
>>
I am not in charge of the www or your access to it. I can get
the bstring.sf.net without difficulty. You can try
bstring.source forge.net which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier
with constness.
websnarl has also been known to assume 32 bit integers and 8 bit
chars.

--
Some informative links:
<news:news.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Sep 26 '06 #11
CBFalconer said:
Richard Heathfield wrote:
>we******@gmail. com said:
... snip ...
>>>
I am not in charge of the www or your access to it. I can get
the bstring.sf.net without difficulty. You can try
bstring.sourc eforge.net which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier
with constness.

websnarl has also been known to assume 32 bit integers and 8 bit
chars.
That's a tempting assumption to make at times. But I can find no definitive
evidence of either assumption in bstrlib (after a very cursory inspection).

I would consider it reasonable to make the 8-bit char assumption provided
that the code checks that the assumption is true and fails to compile if it
is false (#if CHAR_BIT 8 / #error ...), and provided that the reasons for
the assumption are made clear, and provided that guidance is given on what
areas would need looking at if someone were to attempt to remove the
dependence on that assumption.

For example, there are times when you'd sure like to have an array with one
element for each possible unsigned char value. (Boyer-Moore springs to
mind.) If CHAR_BIT is 8, this involves a mere 256 * sizeof(type) bytes. If
CHAR_BIT is 16, however, the overhead becomes significant. And if it's 32,
well... even if it's just an array of unsigned char, you're still talking
16 Gigoctets, which is very significant indeed!

I can see no plausible necessity for assuming that integers have 32 bits,
however. But I stress again that I have seen no definitive evidence that Mr
Websnarf has assumed this. Despite the plethora of 32s in the code, there
are significant indications that he is aware of the fact that integers need
not have 32 bits.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #12
Richard Heathfield wrote:
we******@gmail. com said:
Richard Heathfield wrote:
we******@gmail. com said:
In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage, though.
Yes, you can see evidence of this through CERT's advisory list or even
the lists from Secunia. So others "manage" alright -- they *manage* to
write code that doesn't work.

Um, so what? The existence of people who cannot understand C does not imply
the non-existence of people who can.
But you have not provided any evidence of people who can, outside an
assertion. Where's your solution to the OP's problem? Surely such a
trivial question couldn't pose more effort than it took for you to
write the drivel you've posted in this thread so far.
It's possible using the ordinary C string library too.
We know its *possible*. But the fact that nobody has posted such a
thing is probably evidence of something.

It could just be evidence of DYOH.

<snip>
Every question on c.l.c could be a DYOH question. But this one is easy
to recognize as not being a homework question. Because its really easy
to solve in any other language or using Bstrlib, and is a really
serious pain in the ass to solve in C -- but at the same time there are
really very few fundamental *concepts* being taught in an exercise like
this. The only value of this exercise, to solve in C, is to learn what
a really horridly useless language C is when dealing with strings and
ADTs in general, which most institutions would not bother spending an
exercise on.

Its interesting how you see things so narrowly: there *could* be
programmers that can use the standard libraries string functions
perfectly, therefore that *must* be the right way to do things. Any
question *could* be a homework question, therefore it *is* a homework
question. You know the battery in your computer? It *could* explode;
you better stop using it.
I have written a
string library called "The Better String Library" which is perfectly
suited for this.

Of the two URLs in your sig, one failed to load at all, and the other
presented a bizarre set of cartoon drawings, none of which had any
obvious relation to any kind of string libraries.
I am not in charge of the www or your access to it. I can get the
bstring.sf.net without difficulty. You can try bstring.sourcef orge.net
which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier with
constness.
Well I didn't check it, but I highly doubt that "const" is my problem.
Looking at it again, it could be that some compiler is annoyed by my
coercion of void * to bstring. It should be a warning at most though.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 26 '06 #13
we******@gmail. com said:
Richard Heathfield wrote:
>we******@gmail. com said:
Richard Heathfield wrote:
we******@gmail. com said:
In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage,
though.

Yes, you can see evidence of this through CERT's advisory list or even
the lists from Secunia. So others "manage" alright -- they *manage* to
write code that doesn't work.

Um, so what? The existence of people who cannot understand C does not
imply the non-existence of people who can.

But you have not provided any evidence of people who can, outside an
assertion.
Is it your claim that no such people exist? I offer as obvious
counter-examples the following (non-exhaustive!) list: Dennis Ritchie,
Brian Kernighan, Doug Gwyn, Nick Maclaren, Chris Torek, Lawrence Kirby, Dan
Pop, Chris Dollin, Ben Pfaff, Dann Corbit, Peter Seebach, Clive Feather,
Greg Comeau, and P J Plauger. None of these would have the slightest
problem sorting an array of strings.
Where's your solution to the OP's problem?
I chose not to write one. I am under no obligation to write one. Yes, I
could have provided an example that used my own string library, much as you
did with yours. But that would, IMHO, not have been in keeping with the
OP's request for an ANSI C solution, which suggested to me that he didn't
want a canned response involving a large library. (I accept that I could be
mistaken about that.)

Or I could have coded one up from scratch. I doubt whether there are many
people here (other than, perhaps, yourself) who would question my ability
to do that. It is, after all, a simple enough exercise. Populate the array,
and call qsort. Easy.
Surely such a
trivial question couldn't pose more effort than it took for you to
write the drivel you've posted in this thread so far.
If it's such drivel, why respond to it?
>It's possible using the ordinary C string library too.

We know its *possible*. But the fact that nobody has posted such a
thing is probably evidence of something.

It could just be evidence of DYOH.

<snip>

Every question on c.l.c could be a DYOH question.
Indeed.
But this one is easy to recognize as not being a homework question.
I think not.
Because its really easy
to solve in any other language or using Bstrlib, and is a really
serious pain in the ass to solve in C
....but Bstrlib is written in C! Sheesh, get a life.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #14
Richard Heathfield schrieb:
we******@gmail. com said:
>>pkirk25 wrote:
<snip>
>>>I need an algorithm that takes from this list the names of Realms that
we have both Alliance and Horde data for and to list them in
alphabetic al order. My desired format of this list will be:
Draenor-Alliance
Draenor-Horde
Moonglade-Alliance
Moonglade-Horde
Nordrassil-Alliance
Nordrassil-Horde

So you want to sort the lines of text alphabetically right?
>>>I'm committed to using ANSI C with the K&R book as my guide. It says a
string is an array of chars.

Close. It's a sequence of adjacent characters (typically stored in an array,
yes) that is terminated by the first null character in that sequence.
>>In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage, though.
I _can_ do many things, including reinventing the (polygonal) wheel time
and again. (ISO) C, in my opinion, is not the language to use if you
heavily operate on / with strings and want results quickly.
<snip>
>>By using more advanced strings (i.e., not the crap that the language
comes with),

Not everyone shares websnarf's opinion.
>>this is possible in the C language.

It's possible using the ordinary C string library too.
Yes, it is.
As I wrote in my response to the OP, <4n************ @individual.net >,
C IMO is not the "best" or "easiest" language for this task.
As long as the OP does not suffer through this for learning purposes,
I see no point in restricting oneself to standard C.
I do not care at all for websnarf's acerbic to poisonous tone and
unobjective colouring of his assessments, though.
>>I have written a
string library called "The Better String Library" which is perfectly
suited for this.
<snipped by Richard: Code using bstrlib>

The code looks sufficiently clear, I'd still rather use Perl or python.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 26 '06 #15
Richard Heathfield wrote:
we******@gmail. com said:
Richard Heathfield wrote:
we******@gmail. com said:
Richard Heathfield wrote:
we******@gmail. com said:
In straight C strings are just a rats nest of pain and suffering.

For websnarf, this may well be true. Other people seem to manage,
though.

Yes, you can see evidence of this through CERT's advisory list or even
the lists from Secunia. So others "manage" alright -- they *manage* to
write code that doesn't work.

Um, so what? The existence of people who cannot understand C does not
imply the non-existence of people who can.
But you have not provided any evidence of people who can, outside an
assertion.

Is it your claim that no such people exist?
No, I claim that you have not provided any evidence. Think of it from
the OP's point of view. Suppose he has a question in his mind about
whether or not its even possible to solve the problem in C. How would
you convince him that it was possible? (Well you could always point
him to my post ...)
[...] I offer as obvious
counter-examples the following (non-exhaustive!) list: Dennis Ritchie,
Brian Kernighan, Doug Gwyn, Nick Maclaren, Chris Torek, Lawrence Kirby, Dan
Pop, Chris Dollin, Ben Pfaff, Dann Corbit, Peter Seebach, Clive Feather,
Greg Comeau, and P J Plauger. None of these would have the slightest
problem sorting an array of strings.
Uh ... probably so; I'd even be willing to bet CBFalconer could do it,
but its interesting that you should go to such an "illustriou s list"
for such ridiculously simple problem -- and yet at the same time, still
provide no solution to the problem at hand.

(BTW, I think you have to remove Peter Seebach from that list. I've
seen some of his more marvellous efforts -- programming is not his
strong suit. Assuming you've fixed your internet go check this out:
http://www.seebs.net/ops/ibm/email/sz.c and the sz.h file. Its like
the MS3K of C programming; its so bad its good.)
Where's your solution to the OP's problem?

I chose not to write one. I am under no obligation to write one. Yes, I
could have provided an example that used my own string library, much as you
did with yours. But that would, IMHO, not have been in keeping with the
OP's request for an ANSI C solution,
Uhh... just trying to untangle that logic -- you are saying you have
your own C library for strings that you use that isn't ANSI compliant?
[...] which suggested to me that he didn't
want a canned response involving a large library.
Well that's interesting -- I just don't follow your reasoning at all.
Is there something in the ANSI C language standard that says large
libraries are disallowed?
(I accept that I could be mistaken about that.)
Yathink?
Or I could have coded one up from scratch. I doubt whether there are many
people here (other than, perhaps, yourself) who would question my ability
to do that.
I question the effort required.
[...] It is, after all, a simple enough exercise. Populate the array,
and call qsort. Easy.
Oh yeah, real easy. So easy, you're not even going to do it.
Surely such a
trivial question couldn't pose more effort than it took for you to
write the drivel you've posted in this thread so far.

If it's such drivel, why respond to it?
Because it required no effort? (You're not following along are you?)
It's possible using the ordinary C string library too.

We know its *possible*. But the fact that nobody has posted such a
thing is probably evidence of something.

It could just be evidence of DYOH.

<snip>
Every question on c.l.c could be a DYOH question.

Indeed.
But this one is easy to recognize as not being a homework question.

I think not.
I'm sure Warcraft is really important subject matter for that class
he's taking.
Because its really easy
to solve in any other language or using Bstrlib, and is a really
serious pain in the ass to solve in C

...but Bstrlib is written in C! Sheesh, get a life.
So did you ever take a course in formal logic? I mean seriously,
what's your problem? You know Bstrlib has been written over a period
of several years and I've received lots of feedback on improving it (so
technically some changes and ideas were technically beyond my
thinking/experience). So if I could argue, for a second, that Bstrlib
was a pain in the ass to write, then what exactly is your problem?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 26 '06 #16
Richard Heathfield <in*****@invali d.invalidwrites :
we******@gmail. com said:
[...]
>I am not in charge of the www or your access to it. I can get the
bstring.sf.n et without difficulty. You can try bstring.sourcef orge.net
which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier with
constness.
Really? It compiled for me with no diagnostics.

I used:

gcc -ansi -pedantic -W -Wall -O3 -c bstrlib.c

using gcc versions 2.7.2.2, 3.4.4, and 4.0.2.

--
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.
Sep 26 '06 #17
Keith Thompson said:
Richard Heathfield <in*****@invali d.invalidwrites :
>we******@gmail. com said:
[...]
>>I am not in charge of the www or your access to it. I can get the
bstring.sf.ne t without difficulty. You can try bstring.sourcef orge.net
which is the same thing.

That worked. The compilation didn't, though. You're a bit cavalier with
constness.

Really?
Yes.
It compiled for me with no diagnostics.

I used:

gcc -ansi -pedantic -W -Wall -O3 -c bstrlib.c

using gcc versions 2.7.2.2, 3.4.4, and 4.0.2.
FYI, I used -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-Wno-conversion -ffloat-store -O2 with gcc 2.95.3.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #18
Its always interesting to come back to a newsgroup and see a thread
that has taken on a life of its own.

Fond though I am of World of Warcraft, my project is mainly about
learning C.

Why C instead of C# or Java?

Once upon a time i was a programmer in VB6. I will never touch any
language that is the plaything of a vendor. I still use MSAccess VBA
for projects and find myself wondering why MS decided to kill VB for
those of us who regard OOP as a distraction.

I know from hiring C++ developers that it takes 2 years full time work
experience in C++ to become productive. Even then, C++ developers seem
to have semi-religous practices that require them to abuse other C++
developers. Worse, when one leaves, the "better" his coding standards,
the less likely you are to hire someone who can understand his
object-orientated aspect-guided paradigm-style code and you have to pay
for the whole lot to be done again! So for my hobby project to
automating World of Warcraft gold trading, C++ may well be a better
tool but I don't have 2 full years to devote to any hobby.

C is small - you can learn it it a few weeks - I started last Thursday
and already have something that is usable. Almost every evening, when
I start to code, I watch the damn thing crash. But that feeling of
being able to make it work and my it work ANY which way you choose is
why I chose C. If I wanted to do things the "right" way, I'd be a VB
..NET junkie waiting for MS to decide that VB. NET is a dead end.

I post this in the hope that those who have become very heated about
how best to help me will feel a little better about my bumbling efforts
at reinventing the wheel again and again.

Patrick

Sep 26 '06 #19
"pkirk25" <pa*****@kirks. netwrote in message
news:11******** *************@d 34g2000cwd.goog legroups.com...
Two great answers.

Thanks - if I can make it all fit together I may well become the first
World of Warcraft gold millionaire!
Add:

And p.s. You will get nothing for writing it for me!

--
Mabden
Oct 1 '06 #20

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

Similar topics

5
6489
by: Edward K. Ream | last post by:
Am I reading pep 277 correctly? On Windows NT/XP, should filenames always be converted to Unicode using the mbcs encoding? For example, myFile = unicode(__file__, "mbcs", "strict") This seems to work, and I'm wondering whether there are any other details to consider. My experiments with Idle for Python 2.2 indicate that os.path.join doesn't work as I expect when one of the args is a Unicode string. Everything
2
20535
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
4
6072
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
2
3581
by: Thierry Lam | last post by:
Let's say I have the following xml tag: <para role="source">a & b</para> Currently, an xml parser will treat & as a special character. Does anyone know the special characters to use around the ampersand so that the xml parser can treat "a & b" as a whole value? Thanks Thierry
1
3981
by: st | last post by:
Hi, I'm using xmlDocument.Save(xmlTextWriter) to create an Excel-readable file. All works well, except where I've replaced the carriage return chars in the .innertext to XML-compliant " "; It gets changed to "&amp;#10" and doesn't render new lines in the Excel sheet. Can anyone help? Many thanks,
7
1915
by: theBestFriend | last post by:
If I type in the vualue && from the standard input and store it into myInputString variable, I expected that expression if(myInputString == "&&") will evaluate to true, but it doesn't. Can you please explain why and what do I need to change to make it evaluate to true. I want to treat && as character string, not as a special characters but don't know how. When I type in >= or <=, both expressions if(myInputString == ">=") and...
16
1684
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are somtimes confused about the difference between a two-dimensional array and an array of pointers..." then continues to explain int *b; to be...
23
2976
by: arnuld | last post by:
i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors, with "run-time shown", claim that C++ Library strings are faster than C-style character strings. i wrote the same programme in C & hence found that claim of the authors is *partial*. If we use C-style strings in C++ instead of Library String class, then they are slow but if write the same programme in C then C strings are "faster" than both C++ Library strings & C-style...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10419
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
10201
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...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
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
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
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
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.