|
From: Juliano P. B. C. <jp...@bo...> - 2003-04-04 01:46:30
|
Hello to everybody. I know this isn't a pure c/c++ programming list (=
it
s more a c++ plus dev-c++ programming list, right?) but maybe some of you
could help me with this:=0D
1-Why does gcc for unix tell me "gets" is a dangerous function?=0D
2-What exactly is the difference between the <ENTER> key stroke of unix a=
nd
of windows? I often have problems with it when I read two consecutive val=
ues
for example:=0D
for(i=3D0;i<5;i++)=0D
scanf("%c",&a);=0D
In the example above, I've tried to use "fflush(stdin), but it seemed=
to
have no effect. The one thing that worked fine was something like:=0D
for(i=3D0;i<5;i++)=0D
{=0D
scanf("%c",&a);=0D
getchar(); //strange getchar(). Would it be to read some extra
character of the Unix <ENTER>?=0D
}=0D
I hope my second doubt isn't too confusing.=0D
=0D
Thanks to all.=0D
Juliano=0D
=0D
***********************=0D
Visite tamb=E9m meu site: =0D
http://www.jpavel.kit.net =0D
=20
|
|
From: Daniel K. O. <dan...@ya...> - 2003-04-04 03:48:06
|
> 1-Why does gcc for unix tell me "gets" is a dangerous function?
Simple answer: buffer overflow. Look at fgets.
> 2-What exactly is the difference
> between the <ENTER> key stroke of unix and
> of windows? I often have problems with it when I read two
> consecutive values
> for example: for(i=0;i<5;i++) scanf("%c",&a);
> In the example above, I've tried to use "fflush(stdin), but it seemed to
> have no effect. The one thing that worked fine was something
> like: for(i=0;i<5;i++) { scanf("%c",&a);
> getchar(); //strange getchar(). Would it be to read some extra
> character of the Unix <ENTER>? }
Ioannis said to me once: "fflush empties output buffers, not input". sscanf
doesn't read the remaining '\n'. You should use fgets to read a line
(including the '\n', no need to fflush anything), then sscanf what you need
inside the string.
Daniel K. O.
It's OK to be ignorant; it's not OK to play stupid.
http://www.catb.org/~esr/faqs/smart-questions.html
|
|
From: Ioannis V. <iv...@em...> - 2003-04-04 03:52:44
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Juliano P=E1vel Brasil Cust=F3dio
> Sent: Thursday, April 03, 2003 4:18 PM
> To: Dev-Cpp-Users (dev...@li...
> Subject: [Dev-C++] c on unix
>=20
>=20
> Hello to everybody. I know this isn't a pure c/c++=20
> programming list (it
> s more a c++ plus dev-c++ programming list, right?) but maybe=20
> some of you
> could help me with this:
>=20
> 1-Why does gcc for unix tell me "gets" is a dangerous function?
Because you can't limit the input to the characters that the provided =
buffer
can hold. Example:
#include <stdio.h>
/* ... */
chas s[10];
/* imagine that you enter 50 characters... */
gets(s);
>=20
> 2-What exactly is the difference between the <ENTER> key=20
> stroke of unix and
> of windows? I often have problems with it when I read two=20
> consecutive values
> for example:
>=20
> for(i=3D0;i<5;i++)
>=20
> scanf("%c",&a);
>=20
> In the example above, I've tried to use "fflush(stdin),=20
> but it seemed to
> have no effect.=20
fflush(stdin); is not portable (is not guaranteed to work). fflush() is
intended for *output* streams.
The one thing that worked fine was something like:
>=20
> for(i=3D0;i<5;i++)
>=20
> {
>=20
> scanf("%c",&a);
>=20
> getchar(); //strange getchar(). Would it be to read some extra
> character of the Unix <ENTER>?
>=20
> }
getchar() reads a character including '\n' (*after* '\n' has been =
entered).
In the above code, '\n' remains in the stream (scanf() doesn't read the
'\n').
>=20
> I hope my second doubt isn't too confusing.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|
|
From: Ioannis V. <iv...@em...> - 2003-04-04 05:52:08
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Juliano P=E1vel Brasil Cust=F3dio
> Sent: Thursday, April 03, 2003 4:18 PM
> To: Dev-Cpp-Users (dev...@li...
> Subject: [Dev-C++] c on unix
>=20
>=20
> Hello to everybody. I know this isn't a pure c/c++=20
> programming list (it
> s more a c++ plus dev-c++ programming list, right?) but maybe=20
> some of you
> could help me with this:
>=20
> 1-Why does gcc for unix tell me "gets" is a dangerous function?
Because you can't limit the input to the characters that the provided =
buffer
can hold. Example:
#include <stdio.h>
/* ... */
chas s[10];
/* imagine that you enter 50 characters... */
gets(s);
>=20
> 2-What exactly is the difference between the <ENTER> key=20
> stroke of unix and
> of windows? I often have problems with it when I read two=20
> consecutive values
> for example:
>=20
> for(i=3D0;i<5;i++)
>=20
> scanf("%c",&a);
>=20
> In the example above, I've tried to use "fflush(stdin),=20
> but it seemed to
> have no effect.=20
fflush(stdin); is not portable (is not guaranteed to work). fflush() is
intended for *output* streams.
The one thing that worked fine was something like:
>=20
> for(i=3D0;i<5;i++)
>=20
> {
>=20
> scanf("%c",&a);
>=20
> getchar(); //strange getchar(). Would it be to read some extra
> character of the Unix <ENTER>?
>=20
> }
getchar() reads a character including '\n' (*after* '\n' has been =
entered).
In the above code, '\n' remains in the stream (scanf() doesn't read the
'\n').
>=20
> I hope my second doubt isn't too confusing.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|
|
From: Luigi S. <gi...@li...> - 2003-04-04 12:16:18
|
On Thu, Apr 03, 2003 at 10:18:22AM -0300, Juliano P=E1vel Brasil Cust=F3d=
io wrote:
> [..]
> 1-Why does gcc for unix tell me "gets" is a dangerous function?
You already got an answer to this question
> 2-What exactly is the difference between the <ENTER> key stroke of unix=
and
> of windows? I often have problems with it when I read two consecutive v=
alues
> for example:
> for(i=3D0;i<5;i++)
> scanf("%c",&a);
> In the example above, I've tried to use "fflush(stdin), but it seem=
ed to
> have no effect. The one thing that worked fine was something like:
> for(i=3D0;i<5;i++)
> {
> scanf("%c",&a);
> getchar(); //strange getchar(). Would it be to read some extra
> character of the Unix <ENTER>?
> }
> [..]
The newline character is translated (by the editor you are using) into wh=
at=20
is appropriate for the underlying OS, for instance: LF on Unix/Linux,=20
CRLF on Dos/Windows.
However, I think that this is not the point here, in fact you seem to be
concerned with reading characters from a stream. Probably the following
may be of help.
When you use the format "%c" *all* characters found on the stream are
read (even white-spaces, for instance '\n'), if instead you wish to skip
white-spaces, the format "%1s" should be used.
Example:=20
Suppose that the file "data" contains the following 2 rows:
a b
c d
Remarks: the command "od -cb data" outputs the following 3 rows:
0000000 a b \n c \t d \n
141 040 142 012 143 011 144 012
0000010
Now if you read the file "data" using:
...
char c;
while (scanf("%c", &c) !=3D EOF)
print("'%c'\n", c);
...
You will see echoed all characters (one on each line), including
the space (octal code 040), the newlines and the tab.=20
Instead if you use:
...
char s[] =3D "\0"; /* 2 chars, both are '\0' */
while (scanf("%1s", s) !=3D EOF)
print("'%c'\n", *s);
...
You will only see the letters "a, b, c, d" (one per line).
--=20
>> gisan >>
|