|
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 >>
|