Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #12737 > unrolled thread

fscan: about it

Started byPasquale Frega <zemir_fp@libero.it>
First post2011-11-04 15:41 -0700
Last post2011-11-04 23:57 +0000
Articles 9 — 6 participants

Back to article view | Back to comp.lang.c


Contents

  fscan: about it Pasquale Frega <zemir_fp@libero.it> - 2011-11-04 15:41 -0700
    Re: fscan: about it Ian Collins <ian-news@hotmail.com> - 2011-11-05 11:54 +1300
      Re: fscan: about it Keith Thompson <kst-u@mib.org> - 2011-11-04 23:04 -0700
        Re: fscan: about it Ian Collins <ian-news@hotmail.com> - 2011-11-05 20:12 +1300
          Re: fscan: about it Pasquale Frega <zemir_fp@libero.it> - 2011-11-05 04:47 -0700
            Re: fscan: about it James Kuyper <jameskuyper@verizon.net> - 2011-11-05 08:58 -0400
            Re: fscan: about it Keith Thompson <kst-u@mib.org> - 2011-11-05 13:28 -0700
    Re: fscan: about it Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 16:00 -0700
    Re: fscan: about it John Gordon <gordon@panix.com> - 2011-11-04 23:57 +0000

#12737 — fscan: about it

FromPasquale Frega <zemir_fp@libero.it>
Date2011-11-04 15:41 -0700
Subjectfscan: about it
Message-ID<0636e7d1-01db-4dde-9c89-c84840b9b7d6@hc5g2000vbb.googlegroups.com>
Ok, a simple question: can fscan get a simple return with nothing or a
simple escape or anything?
please try this simple code:
#include <stdio.h>;
#include <math.h>;
int main(void) {
        int i;
        puts("Enter an int number");
        fscan("%d", &i);
        printf("You have entered: &d\n", i);
}
In this case fgets wait for a number or something from keyboard that
is a string o charter too
if you try to press return with nothing it does not go forward
i hope to has be clean

[toc] | [next] | [standalone]


#12739

FromIan Collins <ian-news@hotmail.com>
Date2011-11-05 11:54 +1300
Message-ID<9hj8ovF62fU5@mid.individual.net>
In reply to#12737
On 11/ 5/11 11:41 AM, Pasquale Frega wrote:
> Ok, a simple question: can fscan get a simple return with nothing or a
> simple escape or anything?

I assume you meant scanf?

> please try this simple code:
> #include<stdio.h>;

Don't put a semicolon after an include directive.

> #include<math.h>;
> int main(void) {
>          int i;
>          puts("Enter an int number");
>          fscan("%d",&i);
>          printf("You have entered:&d\n", i);
> }

You really should post something that compiles.

> In this case fgets wait for a number or something from keyboard that
> is a string o charter too
> if you try to press return with nothing it does not go forward
> i hope to has be clean

fgets?

scanf will read until is sees something that isn't white-space or EOF.

You should check the return of (f)scanf, not the value you are trying to 
read.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#12762

FromKeith Thompson <kst-u@mib.org>
Date2011-11-04 23:04 -0700
Message-ID<ln4nyjf6l1.fsf@nuthaus.mib.org>
In reply to#12739
Ian Collins <ian-news@hotmail.com> writes:
> On 11/ 5/11 11:41 AM, Pasquale Frega wrote:
>> Ok, a simple question: can fscan get a simple return with nothing or a
>> simple escape or anything?
>
> I assume you meant scanf?
>
>> please try this simple code:
>> #include<stdio.h>;
>
> Don't put a semicolon after an include directive.
>
>> #include<math.h>;
>> int main(void) {
>>          int i;
>>          puts("Enter an int number");
>>          fscan("%d",&i);
>>          printf("You have entered:&d\n", i);
>> }
>
> You really should post something that compiles.

With gcc, it compiles with a couple of warnings.  By my reading of
6.10.2, an extra semicolon on a #include directive doesn't even violate
a constraint or syntax rule (but it does have undefined behavior).

The typo "&d" for "%d" doesn't even cause undefined behavior; it just
prints the string literal and ignores the second argument.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#12766

FromIan Collins <ian-news@hotmail.com>
Date2011-11-05 20:12 +1300
Message-ID<9hk5vgF62fU12@mid.individual.net>
In reply to#12762
On 11/ 5/11 07:04 PM, Keith Thompson wrote:
> Ian Collins<ian-news@hotmail.com>  writes:
>> On 11/ 5/11 11:41 AM, Pasquale Frega wrote:
>>> Ok, a simple question: can fscan get a simple return with nothing or a
>>> simple escape or anything?
>>
>> I assume you meant scanf?
>>
>>> please try this simple code:
>>> #include<stdio.h>;
>>
>> Don't put a semicolon after an include directive.
>>
>>> #include<math.h>;
>>> int main(void) {
>>>           int i;
>>>           puts("Enter an int number");
>>>           fscan("%d",&i);
>>>           printf("You have entered:&d\n", i);
>>> }
>>
>> You really should post something that compiles.
>
> With gcc, it compiles with a couple of warnings.  By my reading of
> 6.10.2, an extra semicolon on a #include directive doesn't even violate
> a constraint or syntax rule (but it does have undefined behavior).
>
> The typo "&d" for "%d" doesn't even cause undefined behavior; it just
> prints the string literal and ignores the second argument.

c99 /tmp/x.c
"/tmp/x.c", line 1: warning: tokens ignored at end of directive line
"/tmp/x.c", line 2: warning: tokens ignored at end of directive line
"/tmp/x.c", line 6: warning: implicit function declaration: fscan
Undefined                       first referenced
  symbol                             in file
fscan                               x.o

So yes, it "compiles", but it certainly won't produce an executable!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#12771

FromPasquale Frega <zemir_fp@libero.it>
Date2011-11-05 04:47 -0700
Message-ID<eea6ca6f-f72f-4736-b9df-087932cbe79f@er6g2000vbb.googlegroups.com>
In reply to#12766
Yesterday I was very tired and I was very sleepy; of course i intend
"scanf" and no "fscan" or "fgets". The code above i have not compiled
so it is not tested as well as i wrote.
I think it is not possible to get by "scanf" something as a simple
return or an f(1-12) or an escape, as well as anyone of you say. The
";" at the end is intend simply as "end" and i use it simply to
improve reading (as well as in Pascal, you might to write always ";").
No other i intend.
Ok, now i formulate the question in other way:
How can i make a program in manner that i can stop it at the prompt
with ESC, in manner that it can reading a simple return, in manner
that i can set function keys to make something, etc..........?
If no with "scanf", what can i use?

[toc] | [prev] | [next] | [standalone]


#12773

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-11-05 08:58 -0400
Message-ID<j93bu8$314$1@dont-email.me>
In reply to#12771
On 11/05/2011 07:47 AM, Pasquale Frega wrote:
> Yesterday I was very tired and I was very sleepy; of course i intend
> "scanf" and no "fscan" or "fgets". The code above i have not compiled
> so it is not tested as well as i wrote.
> I think it is not possible to get by "scanf" something as a simple
> return or an f(1-12) or an escape,

Use the "%c" format to read a single character. If you want it to only
read in a new-line, but to put any other character back into the
read-buffer, then use the scanset feature: "%1[\n]". Using getchar() and
ungetc() is much simpler way to do this, but you did specify that you
wanted to use scanf().

There's no portable way to identify escape sequences and function keys,
but for any given platform you could use "%c" to read in a character at
a time, and then type the keys that you want it to identify, and find
out how they are represented. Keep in mind that some keys may be
represented by multiple characters.

> ... as well as anyone of you say. The
> ";" at the end is intend simply as "end" and i use it simply to
> improve reading (as well as in Pascal, you might to write always ";").
> No other i intend.

There are many places where you can safely insert a ';' without causing
problems - but be careful not put it anywhere where it doesn't belong,
or would change the meaning of your code. However, inserting a ';' in
places where it doesn't make any difference is going to leave people
suspecting that you don't realize that it's not needed, which is going
to make them very worried about your competence as a C programmer. As
long as you're a newbie, I suppose that's not a bad thing, but I'd
recommend dropping the habit. For experienced C programmers, spurious
';' characters do NOT improve reading.

> Ok, now i formulate the question in other way:
> How can i make a program in manner that i can stop it at the prompt
> with ESC, in manner that it can reading a simple return, in manner
> that i can set function keys to make something, etc..........?
> If no with "scanf", what can i use?

A key problem you need to deal with is buffering. With an unbuffered
stream, as soon as a char becomes available, it can be read by C
character input functions like fgetc(). With line buffering, C input
functions can't retrieve anything until an entire line has been typed
and return has been hit. With full buffering, C input functions do not
see anything until the entire buffer has been filled. C allows full
buffering only if the implementation can be certain that it's not
reading from an input device. Therefore, if stdin is your console, at
worst you're getting line buffering. Unfortunately, what you want is
unbuffered input, and that's not guaranteed - line buffering is much
more common. You can try to make stdin unbuffered by using setbuf(stdin,
NULL), but check for an error return - the standard does not guarantee
that the buffering can be changed. The setbuf() call must occur before
any attempt is made to read from stdin.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#12788

FromKeith Thompson <kst-u@mib.org>
Date2011-11-05 13:28 -0700
Message-ID<lnr51me2l5.fsf@nuthaus.mib.org>
In reply to#12771
Pasquale Frega <zemir_fp@libero.it> writes:
>                                                                  The
> ";" at the end is intend simply as "end" and i use it simply to
> improve reading (as well as in Pascal, you might to write always ";").
> No other i intend.

A semicolon at the end of a #include directive is not merely useless,
it's wrong.  The compiler I use issues a warning and then ignores
it, but other compilers might reject the compilation altogether.

Semicolons in C are not merely decorative.  They're part of the
language syntax, and you need to understand where to use them and
where not to use them.

(The same is true in Pascal, by the way.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#12740

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2011-11-04 16:00 -0700
Message-ID<da1a5029-99e0-40eb-aa41-541434c908f5@m19g2000vbm.googlegroups.com>
In reply to#12737
On Nov 4, 10:41 pm, Pasquale Frega <zemir...@libero.it> wrote:
> Ok, a simple question: can fscan get a simple return with nothing or a
> simple escape or anything?
> please try this simple code:
> #include <stdio.h>;
> #include <math.h>;
> int main(void) {
>         int i;
>         puts("Enter an int number");
>         fscan("%d", &i);
>         printf("You have entered: &d\n", i);}
>
> In this case fgets wait for a number or something from keyboard that
> is a string o charter too
> if you try to press return with nothing it does not go forward
> i hope to has be clean

are you asking if fscanf() can read a number without a following \n?

As another poster stated, you should post code that compiles.

And you can't follow a #include with a ";"

[toc] | [prev] | [next] | [standalone]


#12743

FromJohn Gordon <gordon@panix.com>
Date2011-11-04 23:57 +0000
Message-ID<j91u4i$1g$1@reader1.panix.com>
In reply to#12737
In <0636e7d1-01db-4dde-9c89-c84840b9b7d6@hc5g2000vbb.googlegroups.com> Pasquale Frega <zemir_fp@libero.it> writes:

> Ok, a simple question: can fscan get a simple return with nothing or a
> simple escape or anything?

If you want to abort input when the user presses Enter or Escape,
you don't want to use fscanf.

So, the answer is no.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web