V
Vincenzo Mercuri
Uno said:while((ch = get_char()) > 0) /* yeah, I know... */
{
put_char(ch);
if (ch = 'p')
maybe you meant if (ch == 'p') ?
void put_char(char b)
{
fputc(stdin, b);
}
and fputc(b, stdin);
Uno said:while((ch = get_char()) > 0) /* yeah, I know... */
{
put_char(ch);
if (ch = 'p')
void put_char(char b)
{
fputc(stdin, b);
}
Ben said:Nick Keighley said:On 28 June, 00:17, Uno <[email protected]> wrote:[as re-posted by Nick:]the supposedly broken main()
| int main(void)
| {
| int i;
| if(tty_break() != 0)
| return 1;
| for(i = 0; i < 10; i++)
| printf(" = %d\n", tty_getchar());
| tty_fix();
| return 0;
|
|
|
| }
why?
I agree that it could benefit form a re-write and at least one of the
changes I'd make is C-related and thus topical here. I think people
would be less often surprised if the loop were:
for(i = 0; i < 10; i++) {
printf(" = %d\r\n", tty_getchar());
fflush(stdout);
}
<snip>
Vincenzo said:maybe you meant if (ch == 'p') ?
and fputc(b, stdin);
Vincenzo Mercuri said:maybe you meant if (ch == 'p') ?
and fputc(b, stdin);
Keith said:I really don't think you want to be writing to stdin. (I see Uno
fixed that in a followup.)
1) Seebs isn't insufferable; I suffer him quite gladly. But then, he's
not a fool. I do find fools insufferable.
2) It isn't "your" thread. It's a Usenet thread. You may or may not have
started it (I can't be bothered to look right now), but you don't get to
choose who replies to it.
$ gcc -Wall -Wextra p5.c -o out
$ ./out
k
k
s
s
h
h
p
p$ cat p5.c
#include <stdio.h>
char get_char (void);
void put_char (char);
int
main (void)
{
char ch;
while ((ch = get_char ()) > 0) /* yeah, I know... */
I still don't get what the point of the program is.
But eventually we will make it work out (hopefully) - laughs -
Richard said:Whom are you addressing? I wrote the original comment, but the code that
I originally wrote alongside it has been borken by Uno.
My answer to your question depends on your answer to mine.
Whom are you addressing? I wrote the original comment, but the code that
I originally wrote alongside it has been borken by Uno.
My answer to your question depends on your answer to mine.
ah, I was assuming both the code and the comment were Uno's
Nick Keighley said:and you of course didn't get the type of ch wrong. Testing for >0
rather than != EOF probably isn't actually wrong. The third one was
that soem people (not me!) think assignments in the expression part of
an 'if' are wrong
If I were actually writing a tutorial along these lines, I'd have
thought a lot harder about that, and probably come up with some example
programs that didn't include getchar!
> Very true - but I was kinda stuck for a good solution, given the
> enforced absence of standard headers (which necessarily meant that I
> didn't have a definition for EOF). I could have used >= 0, of course,
> and that would perhaps have been a marginal improvement.
>
Richard Heathfield wrote:
While I was flailing about with this stuff, trying to attain a standard
solution with the tools you're stuck with I wrote this:
#include <stdio.h>
char get_char (void);
void put_char (char);
int main (void)
{
char ch;
while ((ch = get_char ()) > 0) /* yeah, I know... */
{
put_char (ch);
if ((ch == 'p'))
break;
}
return 0;}
void put_char (char b)
{
fputc (b, stdout);
}
char get_char (void)
{
char c;
c = fgetc (stdin);
return c;
} [...]
If you work this up into your own tutorial, you can use this as an
example of something that doesn't quite cut it.
It's all legal C, but
this program doesn't have the fflush functionality, and in that respect
is just like the program in the faq that seebs loves, but this person
thinks needs to be fflushed down the toilet.
> Very true - but I was kinda stuck for a good solution, given the
> enforced absence of standard headers (which necessarily meant that I
> didn't have a definition for EOF). I could have used >= 0, of course,
> and that would perhaps have been a marginal improvement.
>
stdio.h is by my estimation the hardest of the standard libraries to
implement. (Plauger's hardest (by his own estimation) was math.h) One
of the macros that must be defined, like Keith says, is EOF. These are
the macros that Plauger uses in stdio.h:
/* macros */
#define NULL _NULL
#define _IOFBF 0
I think this listing is much cleaner than you would find if you delved
into your own machine's stdio.h, which, as far as I can tell, is a
non-solvable progression of #included files.
I would also like to bring up how Plauger deals with system-specific stuff:
/* stdio.h standard header */
#ifndef _STDIO
#define _STDIO
#ifndef _YVALS
#include <yvals.h>
#endif
He puts them in a header, tells the reader it is all the nasty bits, and
then continues. I find it strange that people can't comment on a
question with the premise that they're to avoid the non-standard header.
what?
I still haven't found yvals.h yet. I've been reading the book for
months now.
it's a while since I read it. I'm pretty sure it's in there. Have you
tried the index?
pete said:Uno said:char
get_char (void)
{
char c;
c = fgetc (stdin);
return c;
}
// gcc -Wall -Wextra p5.c -o out
$
If you work this up into your own tutorial, you can use this as an
example of something that doesn't quite cut it. It's all legal C, but
this program doesn't have the fflush functionality,
and in that respect
is just like the program in the faq that seebs loves, but this person
thinks needs to be fflushed down the toilet.
Concerning:
char c;
c = fgetc (stdin);
char is the wrong type to store the return value of fgetc.
N869
int fgetc(FILE *stream);
Returns
[#3] If the end-of-file indicator for the stream is set, or
if the stream is at end-of-file, the end-of-file indicator
for the stream is set and fgetc returns EOF.
INT !
you're trolling aren't you?
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.