user input, getchar, and buffer - For C beginners and those withteaching skills...

P

pascal360

You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before
posting.
You do not have the permission required to post.


Phil Carmody
View profile
More options Aug 22, 12:53 pm
Newsgroups: comp.lang.c
From: Phil Carmody <[email protected]>
Date: Sat, 22 Aug 2009 13:53:17 +0300
Local: Sat, Aug 22 2009 12:53 pm
Subject: Re: Can C do it ?
Reply | Reply to author | Forward | Print | Individual message | Show
original | Report this message | Find messages by this author

- Hide quoted text -
- Show quoted text -
[email protected] (Richard Tobin) said:
All those shells are probably putting the terminal in raw mode (or at
least a not-fully-cooked mode) and interpreting the characters
themselves.

Absolutely. Context is everything. "under Unix" wasn't quite
enough context to isolate the behaviour posited.
Try it inside "cat" on each system. It should
echo the line after the first one, and exit after the second.

In that context, all of the above perform exactly as you state.

Phil
 
P

pascal360

Willem
View profile
More options Aug 22, 1:32 pm
Newsgroups: comp.lang.c
From: Willem <[email protected]>
Date: Sat, 22 Aug 2009 11:32:32 +0000 (UTC)
Local: Sat, Aug 22 2009 1:32 pm
Subject: Re: Can C do it ?
Reply | Reply to author | Forward | Print | Individual message | Show
original | Report this message | Find messages by this author
Phil Carmody wrote:

) Absolutely. Context is everything. "under Unix" wasn't quite
) enough context to isolate the behaviour posited.

How about 'a C program, using getchar(), under Unix' ?
I believe getchar() was in the original context.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't
you !
#EOT
 
P

pascal360

Willem said:
Phil Carmody wrote:
) Absolutely. Context is everything. "under Unix" wasn't quite
) enough context to isolate the behaviour posited.
How about 'a C program, using getchar(), under Unix' ?
I believe getchar() was in the original context.

Presuming cat uses getchar as it reproduces the effect at the
console described demonstrates that your guess is quite wrong:

asdf ~ echo -e '\004\004not seen' | cat
not seen

This is a property of the terminal passing characters to the programs,
and not a property of the programs that finally sink the characters
themselves.

Phil
 
B

Beej Jorgensen

Just to say, I'm allergic to pointer, i'm just very carefull as i know
they are very powerfull

Don't build them up to more than they are; pointers aren't some kind of
high-powered sorcery.

It's not really any more mysterious than your home (a thing) and your
home address (where that thing is located, a pointer to it).

-Beej
 
K

Keith Thompson

As a newbie with C, i find user's input with C somehow difficult to
understand and implement. I have gathered posts from "Can C do it? "
thread in this forum that i find very interesting for learning and
deepen this learning or go further into details.

Pascal

You've apparently copy-and-pasted a couple dozen articles without
crediting the authors of those articles.

Have you read section 12 of the comp.lang.c FAQ? Was it helpful?
 
B

bpascal123

Hi,

Yes, i have asked myself that question but as i followed what i
thought would be coherent rules :

- I wouldn't say copy/paste , i have just just gathered information
into a coherent thread (with the same subject going all the way)

- That thread remained in comp.lang.c and it's not like messages went
to another website outside usenet

- I did copy paste or moved (althought that option is not made
available) all the infos from the authors of the message and above all
who wrote the message and who was the originator of a post displayed
in a replied)...

- I really didn't expect someone who posts in usenet, would mind to
see his message published in another thread with his name and infos
and all quotes needed to he is the author


I understand i took an initiative and didn't read usenet rules before.
Had i had to ask anyone's permission before, i wouldn't have done it,
the thread would have been too difficult to reconstitute
chronologicly.

SO KEITH (OR ANYONE READING THIS), JUST TELL ME IF YOU A POST THAT WAS
YOURS I HAVE COPIED/PASTE TO BE REMOVED

Thx,

Pascal
 
B

bpascal123

Don't build them up to more than they are; pointers aren't some kind of
high-powered sorcery.

It's not really any more mysterious than your home (a thing) and your
home address (where that thing is located, a pointer to it).

-Beej

There was a post that said it's a typo mistake (i for NOT in i am
NOT...) and that I am not allergic to pointers !
 
B

bpascal123

I should have used an anonymous name to post so this thread and
initiative would have been more transparent...I can remove this thread
and redo it under an anymous posting thread, i'm not sure if it's
something possible but it would closer to the original thread...
 
J

jameskuyper

Hi,

Yes, i have asked myself that question but as i followed what i
thought would be coherent rules :

- I wouldn't say copy/paste , i have just just gathered information
into a coherent thread (with the same subject going all the way)

- That thread remained in comp.lang.c and it's not like messages went
to another website outside usenet

- I did copy paste or moved (althought that option is not made
available) all the infos from the authors of the message and above all
who wrote the message and who was the originator of a post displayed
in a replied)...

You have included junk that was not part of the message itself, that
was part of the Google Groups message display format. For instance,
the message you quoted from me says:
James Kuyper
View profile
(1 user) More options Aug 21, 1:32 pm ....
Reply | Reply to author | Forward | Print | Individual message | Show
original | Report this message | Find messages by this author

You shouldn't copy that junk; it just causes confusion. If for some
reason you feel a need to repost other people's messages, I recommend
clicking on "Show original" and copying that stuff.
- I really didn't expect someone who posts in usenet, would mind to
see his message published in another thread with his name and infos
and all quotes needed to he is the author


I understand i took an initiative and didn't read usenet rules before.

There are conventions for quoting other people's text, and you haven't
followed them. Interpreted literally according to those conventions,
you have been implicitly claiming authorship of the material you've
copied from others (including the material you've copied from your
newsreader interface). This interpretation doesn't hold up under even
a single moment's thought, but the fact that you're violating
quotation conventions is a barrier to understanding the purpose of
your messages (just one among many, unfortunately).

However, the biggest reason why you shouldn't have done this is that,
because you've moved these messages out of their proper location in
the proper thread, it's difficult to trace the context of these
messages. Those messages contain references back to previous messages
in the same thread, and any decent newsreader can use those references
to display the messages in a way that makes those connections clear.
You've broken those connections.

Selecting out these messages, and not others, and posting them in the
order in which you've reposted them, serves some purpose that is
important to you; in the absence of commentary from you, it's not
clear what that purpose is. If you feel that you must repost such
messages, you should add commentary to each message explaining why
you've selected it for reposting, and how it fits in with the other
messages you're reposting. This ties in with the previous comment
about quoting conventions - if you properly quote the material you're
copying, it will be easy to distinguish between what you've copied and
your commentary about it.
 
J

jameskuyper

I should have used an anonymous name to post so this thread and
initiative would have been more transparent...I can remove this thread
and redo it under an anymous posting thread, i'm not sure if it's
something possible but it would closer to the original thread...

I don't see any advantage in you making these reposts anonymously; but
if you choose to do so, could you identify in advance which identity
you will be using? That way, those of us who are so inclined can
killfile that identity before we even see the stuff.
 
K

Keith Thompson

I should have used an anonymous name to post so this thread and
initiative would have been more transparent...I can remove this thread
and redo it under an anymous posting thread, i'm not sure if it's
something possible but it would closer to the original thread...

No, you can't remove the thread. Most news servers ignore cancel
messages (the alternative would be that most Usenet articles would
quickly vanish due to forged cancellations). It's out there,
and it's not going away.

What exactly would be the point of doing the same thing again
anonymously?

I think your intent was to provide a summary of information about
how to read input in C. I beleive section 10 of the comp.lang.c
FAQ serves that same purpose more coherently.
 
N

Nobody

The code above is about cleaning the buffer from '\n' until EOF ( EOF
= CTRL-Z or D ?)

EOF is whatever the OS says it is.

On Unix, the terminal (tty) driver generates an end-of-file condition when
a particular key is pressed (Ctrl-D is the usual default, but it's
configurable with e.g. "stty eof <char>").

DOS and the Windows command prompt generate end-of-file for Ctrl-Z. They
also treat a Ctrl-Z character within a file as end-of-file for files
opened in text mode (this was inherited from CP/M, which didn't record the
number of bytes in a file).
 
B

Ben Bacarisse

On Aug 25, 2:11 am, pete <[email protected]> wrote:

Cut the parts you don't want to comment on. This almost always
includes the poster's sig block.
I can't remember where, i have read that fflush would impact the
buffer with less control than
int c;
while ((c = while()) != '\n' && c != EOF)) {}

You mean getchar rather than the second while. The trouble with using
fflush(stdin) is that it is not portable. Some systems provide it as
an extension but even if it is permitted, how can you be sure it does
what you want?
 
K

Keith Thompson

Ben Bacarisse said:
You mean getchar rather than the second while. The trouble with using
fflush(stdin) is that it is not portable. Some systems provide it as
an extension but even if it is permitted, how can you be sure it does
what you want?

In fact the behavior of fflush(stdin) is undefined. Some
implementations define useful behavior for it, but in general it
should be avoided.
 
B

bpascal123

Hi,

If someone has an idea, again about user input.
The code is located there :

http://www.ltam.lu/Tutoriel_Ansi_C/homesol.htm

It works as it is on Windows but not on Linux. I changed gets to fgets
and it would work partly on linux like only if the end of the string
matches the 2nd input request.

Quickly :the code : it's to delete a character or a string of
characters entered by the user (Obj) from another string (Suj) also
entered by the user.

Using :
printf("Introduisez la chaîne à supprimer : ");
gets(OBJ);
printf("Introduisez la chaîne à transformer : ");
gets(SUJ);

In Windows it fully works.

To use it under Linux but keeping it work under Windows, the change
that comes to my mind as a beginner :

printf("\n\nIntroduisez la chaine a transformer : ") ;
fgets(Suj, sizeof(Suj), stdin);
printf("\n\nIntroduisez la chaine a supprimer : ") ;
fgets(Obj, sizeof(Obj), stdin);

Running it in Windows makes it partly work : for example in this
string : "bcdef", if "cd" is Obj (or the string to delete), in
Windows, it won't work, but if it's "ef" the last characters from the
string, it's working.

What Windows does with "cd" ?
How to make it work ? ( I haven't tried with scanf("%s",Obj); because
i've read scanf has no protection against overflow.
Should I use a spefic function such as the one seen earlier in this
thread : purger() and clear() ?

Thanks,
 
F

Flash Gordon

Hi,

If someone has an idea, again about user input.
The code is located there :

http://www.ltam.lu/Tutoriel_Ansi_C/homesol.htm

You don't say which of the many exercises and I doubt that many people
will hunt through them.
It works as it is on Windows but not on Linux. I changed gets to fgets
and it would work partly on linux like only if the end of the string
matches the 2nd input request.

Ah, but did you allow for the newline character fgets normally leaves?
Or the situation where it doesn't leave a newline? Read the definition
of fgets in your text book.
Quickly :the code : it's to delete a character or a string of
characters entered by the user (Obj) from another string (Suj) also
entered by the user.

Using :
printf("Introduisez la chaîne à supprimer : ");
gets(OBJ);
printf("Introduisez la chaîne à transformer : ");
gets(SUJ);

That is of (almost) no use without the definitions of OBJ and SUJ. Also,
it is generally considered to be bad practice to name variables in upper
case.
In Windows it fully works.

To use it under Linux but keeping it work under Windows, the change
that comes to my mind as a beginner :

printf("\n\nIntroduisez la chaine a transformer : ") ;
fgets(Suj, sizeof(Suj), stdin);
printf("\n\nIntroduisez la chaine a supprimer : ") ;
fgets(Obj, sizeof(Obj), stdin);

<snip>

Now check the return value, check if there is a newline, and handle it.

Also, before replacing one function with another check the definitions
of both in your text book.
 
N

Nick Keighley

(e-mail address removed) said:

you are just going to ignore the advice not to use all upper case
in variable names aren't you?
Stop right there. gets is a buffer overrun waiting to happen.

that is, gets() has no way of specifying the length of the buffer
(OBJ)
hence it cannot tell if too many charcaters have been entered. gets()
has been the cause of many exploits and much malware (virii).

Check the archives (if google have got off their backsides and fixed
them)
for more details on get()

<snip>
 

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,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top