Histogram of character frequencies

R

RoS

In data Sun, 9 Dec 2007 00:10:13 -0800 (PST), William Pursell
scrisse:
None of the above. Try:

why "none"?
while(1)
{int c=getchar();
if(feof(stdin)||ferror(stdin)) break;
.....
}

at last should be ok
 
J

James Kuyper

RoS said:
In data Sun, 9 Dec 2007 00:10:13 -0800 (PST), William Pursell
scrisse:


why "none"?
while(1)
{int c=getchar();
if(feof(stdin)||ferror(stdin)) break;
....
}

It will work, but it's not the right way to do it. There's no point in
wasting time checking feof() or ferror() unless c is EOF, something your
code doesn't even bother to check for. As a secondary matter, your way
of dealing with this treats feof(), which is normal, the same way as
ferror(), which indicates that something has gone wrong. That's
generally a bad design decision.
 
O

O_TEXT

Chris Torek a écrit :

Okay. English language use undefined words to deals with undefined concept.

However, I use the word byte to deal with the translation of what is
called «octet» in french language.

This URL on a page which deals of bytes which are not eight bits seams
to be C spcecific. Isn't it?

For example, in java language, a byte is a eight bits data.

Sure: a number of IBM mainframes use EBCDIC.

Okay. But is is for very few computers isn't it?

Did IBM planned to migrate their computer, to EBCDIC-UTF8?

Did IBM mainframes activity be bought by Lenovo?
It is mostly a matter of finding assumptions about character sets
(such as the assumption that "all alphabetic characters are
contiguous") and cleaning them up. There is usually a tradeoff
involved -- you may need an extra lookup table here or there, for
instance --

I do not understand well neither this point...
but in general the cost of accomodating different
"native text" encodings is relatively low, especially when compared
with the cost of accomodating UTF-8, UTF-16, Unicode, and/or
"internationalization".

.... nor this one.
Might be because I believe that accommodating is done by Unicode.
 
J

James Kuyper

O_TEXT said:
Chris Torek a écrit :

Okay. English language use undefined words to deals with undefined concept.

However, I use the word byte to deal with the translation of what is
called «octet» in french language.

It is also called an octet in English. However, most people are
unfamiliar with that word, and use byte as if it were a synonym for
octet. The C standard clearly says otherwise, and it is the C standard's
meaning that should be used when discussing C, but even most C
programmers are unaware of the distinction.
This URL on a page which deals of bytes which are not eight bits seams
to be C spcecific. Isn't it?

Okay. But is is for very few computers isn't it?

It doesn't matter. The C standard was deliberately designed to leave a
great deal of room for implementors to choose whichever character
encoding they wish. Unnecessarily building a reliance on a particular
character encoding into your program is a bad idea, even if it is
overwhelmingly the most popular encoding. And, while character sets that
are (to varying degrees) compatible with one or more of the national
variants of ASCII are indeed commonplace, there's nothing close to a
single universally accepted character set at this time.
... nor this one.
Might be because I believe that accommodating is done by Unicode.

Accommodating systems that don't support Unicode is also an issue.
 
P

Peter 'Shaggy' Haywood

Groovy hepcat RoS was jivin' in comp.lang.c on Sun, 9 Dec 2007 5:51 pm.
It's a cool scene! Dig it.
In data Fri, 07 Dec 2007 00:52:19 +1100, Peter 'Shaggy' Haywood
scrisse:


so what is right?

while(1)
{
c = getchar();
if( feof(stdin) || ferror(stdin) ) break;
putchar(c);
}

or

while(1)
{
c = getchar();
if( feof(stdin) ) break;
putchar(c);
}

while(1)
{
c = getchar();
if( ferror(stdin) ) break;
putchar(c);
}

or no one of above

Neither of the scenarios you have given. But I have shown you what to
do. Please read the portion of my previous reply that I have quoted
above (beginning with "What you should be doing is this:"). Here's how
it works:

while(EOF != (c = getchar()))

Here we read in a byte with getchar(). If the read fails, getchar()
returns EOF (which is a negative integer constant that compares unequal
to any valid character value). Each iteration of the loop, its
controlling expression tests for this value and, if EOF is detected,
quits the loop without entering the body.

{
putchar(c);
}

Here in the body of the loop (if entered), all we do is output the
character.
It may help you to understand what's going on if I write it a slightly
different way. The loop as shown above is equivalent to this:

c = getchar();
while(EOF != c)
{
putchar(c);
c = getchar();
}

First we read a character. Next we test whether the read succeeded by
testing for EOF. If the read was successful, the body is entered, and
we output the character then read another. We go back to the top of the
loop and test whether that read was successful. If so, we enter the
body, output the character and read another. And so on.

if(!feof(stdin))

Once we've quit looping, we then use feof() to test for an end of file
condition. If feof() does not return true, then we're not at the end of
the file/stream. In that case, the read failure must have been due to
an error, so we handle it:

{
/* File read error: handle it somehow. */
}

Alternatively, you could use ferror() to test for an error in the
stream, rather than using feof(). This is the method I prefer.

if(ferror(stdin))
{
/* File read error: handle it somehow. */
}

This approach also works with functions that read more than one byte
at a time, such as fprintf() and fread(). For example:

char buf[100];

while(NULL != fgets(buf, sizeof buf, stdin))
{
fputs(buf, stdout);
}
if(ferror(stdin))
{
/* File read error: handle it somehow. */
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top