Is getopt() and its companions, commonly found in GNU libc and other
Unices libc, part of the C standard?
Another doubt: I have a switch inside a while loop; is there a way to
break out of the loop from the switch without using goto? I mean:
start:
while(chr = fgetc(inputfile))
{
switch(chr)
{
case 'a': case 'b':
do_one_stuff(chr);
break;
case 'c': case 'd':
do_some_other_stuff(chr);
break;
case 'e': case 'f':
do_stuff(chr);
break;
default:
goto start;
} /* end switch */
do_even_more_stuff_here();
} /* end while */
Is there a way to get out of the while (i.e., skipping the
"do_even_more_stuff_here();" part) without using this goto?
Thank in advance.
--
Quidquid latine dictum sit altum viditur
|
|
0
|
|
|
|
Reply
|
jose_de_paula (59)
|
1/30/2004 2:29:59 PM |
|
Jos� de Paula <jose_de_paula@ig.com.br> scribbled the following:
> Is getopt() and its companions, commonly found in GNU libc and other
> Unices libc, part of the C standard?
No.
> Another doubt: I have a switch inside a while loop; is there a way to
> break out of the loop from the switch without using goto? I mean:
> start:
> while(chr = fgetc(inputfile))
> {
> switch(chr)
> {
> case 'a': case 'b':
> do_one_stuff(chr);
> break;
> case 'c': case 'd':
> do_some_other_stuff(chr);
> break;
> case 'e': case 'f':
> do_stuff(chr);
> break;
> default:
> goto start;
> } /* end switch */
> do_even_more_stuff_here();
> } /* end while */
> Is there a way to get out of the while (i.e., skipping the
> "do_even_more_stuff_here();" part) without using this goto?
You could use some sort of flag that you set before the break, and
check before the do_even_more_stuff_here(). If the flag is true,
you can then break or continue the while loop.
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Insanity is to be shared."
- Tailgunner
|
|
0
|
|
|
|
Reply
|
palaste (2323)
|
1/30/2004 2:42:55 PM
|
|
In other news, Jos� de Paula <jose_de_paula@ig.com.br> typed:
> Another doubt: I have a switch inside a while loop; is there a way to
> break out of the loop from the switch without using goto? I mean:
>
I'd do:
> while(chr = fgetc(inputfile))
> {
int should_break_out = 0;
> switch(chr)
> {
> case 'a': case 'b':
> do_one_stuff(chr);
> break;
> case 'c': case 'd':
> do_some_other_stuff(chr);
> break;
> case 'e': case 'f':
> do_stuff(chr);
> break;
> default:
should_break_out = 1;
break;
> } /* end switch */
if( should_break_out )
break;
> do_even_more_stuff_here();
> } /* end while */
>
> Is there a way to get out of the while (i.e., skipping the
> "do_even_more_stuff_here();" part) without using this goto?
>
--
runtime
|
|
0
|
|
|
|
Reply
|
spam2243 (1)
|
1/30/2004 2:44:27 PM
|
|
José de Paula wrote:
> Is getopt() and its companions, commonly found in GNU libc and other
> Unices libc, part of the C standard?
>
No, but it is part of POSIX (which is OT in this newsgroup).
> Another doubt: I have a switch inside a while loop; is there a way to
> break out of the loop from the switch without using goto? I mean:
>
> start:
> while(chr = fgetc(inputfile))
> {
> switch(chr)
> {
> case 'a': case 'b':
> do_one_stuff(chr);
> break;
> case 'c': case 'd':
> do_some_other_stuff(chr);
> break;
> case 'e': case 'f':
> do_stuff(chr);
> break;
> default:
> goto start;
> } /* end switch */
> do_even_more_stuff_here();
> } /* end while */
>
> Is there a way to get out of the while (i.e., skipping the
> "do_even_more_stuff_here();" part) without using this goto?
>
What you really want is not break out of the while, but continue (since you
say goto start which is the start of the while loop). A continue will work
just fine as it doesnt have special meaning inside the switch.
If you really want to break out of the while loop, no real easy and elegant
way. You'll have to maintain a possibly useless separate flag and
unnecessarily check it as part of your while condition. A goto might be
cleaner and efficient. Don't be dogmatic in rejecting programming
constructs.
-nrk.
> Thank in advance.
>
--
Remove devnull for email
|
|
0
|
|
|
|
Reply
|
ram_nrk20002 (148)
|
1/30/2004 3:14:32 PM
|
|
Jos� de Paula <jose_de_paula@ig.com.br> spoke thus:
> while(chr = fgetc(inputfile))
Aside from what others have said, this is incorrect: fgetc() returns
EOF if it fails (whether because of EOF or an error), and you should
test chr against it explicitly. I do hope chr is declared as an int,
by the way.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
|
|
0
|
|
|
|
Reply
|
ataru (1609)
|
1/30/2004 4:10:08 PM
|
|
In <bvdqhf$gfi$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
>Jos� de Paula <jose_de_paula@ig.com.br> scribbled the following:
>> Is getopt() and its companions, commonly found in GNU libc and other
>> Unices libc, part of the C standard?
>
>No.
But it can be implemented in standard C and used anywhere a standard C
implementation is available.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
|
|
0
|
|
|
|
Reply
|
Dan.Pop (3615)
|
1/30/2004 4:14:20 PM
|
|
On 2004-01-30, nrk <ram_nrk2000@devnull.verizon.net> wrote:
> Jos� de Paula wrote:
>
>> Is getopt() and its companions, commonly found in GNU libc and other
>> Unices libc, part of the C standard?
>>
>
> No, but it is part of POSIX (which is OT in this newsgroup).
>
>><snip goto meaning "continue">
>
> What you really want is not break out of the while, but continue (since you
> say goto start which is the start of the while loop). A continue will work
> just fine as it doesnt have special meaning inside the switch.
Yes, what I want is continue, thanks. So in this case I could just use
continue? It feels counter-intuitive, because I'm used to the fact that,
syntactically (sp?), break and continue are the same thing, even though
continue doesn't have any meaning inside switch blocks.
>
> If you really want to break out of the while loop, no real easy and elegant
> way. You'll have to maintain a possibly useless separate flag and
> unnecessarily check it as part of your while condition. A goto might be
> cleaner and efficient. Don't be dogmatic in rejecting programming
> constructs.
Yeah, sure. In this case I don't see the goto as a big problem, because
the label is near the goto, and it's clear what the code does. I rather
prefer it than using the extra variable.
--
Quidquid latine dictum sit altum viditur
|
|
0
|
|
|
|
Reply
|
jose_de_paula (59)
|
1/30/2004 7:06:40 PM
|
|
José de Paula wrote:
> On 2004-01-30, nrk <ram_nrk2000@devnull.verizon.net> wrote:
>> José de Paula wrote:
>>
>>> Is getopt() and its companions, commonly found in GNU libc and other
>>> Unices libc, part of the C standard?
>>>
>>
>> No, but it is part of POSIX (which is OT in this newsgroup).
>>
>
>>><snip goto meaning "continue">
>
>>
>> What you really want is not break out of the while, but continue (since
>> you say goto start which is the start of the while loop). A continue
>> will work just fine as it doesnt have special meaning inside the switch.
>
> Yes, what I want is continue, thanks. So in this case I could just use
> continue?
Yes.
> It feels counter-intuitive, because I'm used to the fact that,
> syntactically (sp?), break and continue are the same thing, even though
> continue doesn't have any meaning inside switch blocks.
>
Well, break and continue are not the same thing at all semantically. One
takes you out of the loop while the other just skips the rest of the code
for the current iteration of the loop. I agree with you on the
counter-intuitive part, as I also see break/continue as a pair that often
go together. The problem probably stems from a lazy compiler writer in the
early days who decided to re-use break for a somewhat similar purpose
instead of introducing a new keyword for use inside switch statements :-)
See Chris Torek's excellent recent post on switch statements for a peek
under the hood:
<bukmei08bh@enews3.newsguy.com>
-nrk.
>>
>> If you really want to break out of the while loop, no real easy and
>> elegant
>> way. You'll have to maintain a possibly useless separate flag and
>> unnecessarily check it as part of your while condition. A goto might be
>> cleaner and efficient. Don't be dogmatic in rejecting programming
>> constructs.
>
> Yeah, sure. In this case I don't see the goto as a big problem, because
> the label is near the goto, and it's clear what the code does. I rather
> prefer it than using the extra variable.
>
--
Remove devnull for email
|
|
0
|
|
|
|
Reply
|
ram_nrk20002 (148)
|
1/30/2004 7:55:26 PM
|
|
Jos� de Paula <jose_de_paula@ig.com.br> wrote in message news:<pan.2004.01.30.14.29.56.702142@ig.com.br>...
> Is getopt() and its companions, commonly found in GNU libc and other
> Unices libc, part of the C standard?
Can someone post some working code snippet using getopt()?
Thanks,
Nimmi
|
|
0
|
|
|
|
Reply
|
nimmi_srivastav (58)
|
1/30/2004 10:01:42 PM
|
|
Em Fri, 30 Jan 2004 15:01:42 -0800, Nimmi Srivastav escreveu:
> Jos� de Paula <jose_de_paula@ig.com.br> wrote in message news:<pan.2004.01.30.14.29.56.702142@ig.com.br>...
>> Is getopt() and its companions, commonly found in GNU libc and other
>> Unices libc, part of the C standard?
>
> Can someone post some working code snippet using getopt()?
>
As you wish:
char ch, *flag1, *flag2;
while ( (ch = getopt(argc, argv, "o:f:h")) != -1)
{
switch (ch)
{
case 'o':
flag1 = optarg;
break;
case 'f':
flag2 = optarg;
break;
case 'h':
usage(stdout, EXIT_SUCCESS, argv[0]);
break;
case '?':
default:
usage(stderr, EXIT_FAILURE, argv[0]);
break;
}
}
Here optarg is a global variable defined by libc; it contains the argument
for the option. If you are under a Unix system (*BSD, Linux et al.), man 3
getopt should enlighten you. However, as pointed elsewhere in this thread,
getopt() is not portable outside Unix systems.
--
Quidquid latine dictum sit altum viditur
|
|
0
|
|
|
|
Reply
|
jose_de_paula (59)
|
1/30/2004 10:17:44 PM
|
|
Jos� de Paula <jose_de_paula@ig.com.br> wrote in message news:<pan.2004.01.30.14.29.56.702142@ig.com.br>...
> Is getopt() and its companions, commonly found in GNU libc and other
> Unices libc, part of the C standard?
>
Even thought getopt is not part of the standard C library, it is used
quite extensively in the industry for parsing command line arguments.
If you don't have access to the source code, here's one site that has
an implementation for getopt:
http://www.utexas.edu/ftp/source/utilities/getopt/getopt.c
Here's some additional information:
http://www.rahul.net/cgi-bin/userbin/man?topic=getopt§ion=3
Hope that helps!
Sandeep
|
|
0
|
|
|
|
Reply
|
sandeep6699 (10)
|
1/30/2004 10:21:18 PM
|
|
Em Fri, 30 Jan 2004 15:21:18 -0800, Sandeep Sharma escreveu:
> Jos� de Paula <jose_de_paula@ig.com.br> wrote in message news:<pan.2004.01.30.14.29.56.702142@ig.com.br>...
>> Is getopt() and its companions, commonly found in GNU libc and other
>> Unices libc, part of the C standard?
>>
>
> Even thought getopt is not part of the standard C library, it is used
> quite extensively in the industry for parsing command line arguments.
> If you don't have access to the source code, here's one site that has
> an implementation for getopt:
> http://www.utexas.edu/ftp/source/utilities/getopt/getopt.c
>
Thank you, but the Source is with me, Luke! :-) The problem is that if I
want to write code that is portable across different operating systems
I'll have to include the wheel (i.e., an implementation of getopt()) with
it.
>
> Hope that helps!
It does, thank you.
--
Quidquid latine dictum sit altum viditur
|
|
0
|
|
|
|
Reply
|
jose_de_paula (59)
|
1/30/2004 10:29:28 PM
|
|
Jos� de Paula wrote:
> Em Fri, 30 Jan 2004 15:21:18 -0800, Sandeep Sharma escreveu:
> > Jos� de Paula <jose_de_paula@ig.com.br> wrote in message
>
> >> Is getopt() and its companions, commonly found in GNU libc and
> >> other Unices libc, part of the C standard?
> >
> > Even thought getopt is not part of the standard C library, it is
> > used quite extensively in the industry for parsing command line
> > arguments. If you don't have access to the source code, here's
> > one site that has an implementation for getopt:
> > http://www.utexas.edu/ftp/source/utilities/getopt/getopt.c
> >
> Thank you, but the Source is with me, Luke! :-) The problem is
> that if I want to write code that is portable across different
> operating systems I'll have to include the wheel (i.e., an
> implementation of getopt()) with it.
And that includes asking any questions beyond "is it standard"
about it on c.l.c. The following is a perfectly standards
compliant getopt:
int getopt(char *p)
{
if (p) return (int)p & 3;
return 0;
} /* getopt */
and somehow I suspect that is not what you are talking about.
--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
1/31/2004 3:43:29 AM
|
|
In <pan.2004.01.30.22.29.24.686412@ig.com.br> =?iso-8859-1?q?Jos=E9_de_Paula?= <jose_de_paula@ig.com.br> writes:
>Thank you, but the Source is with me, Luke! :-) The problem is that if I
>want to write code that is portable across different operating systems
>I'll have to include the wheel (i.e., an implementation of getopt()) with
>it.
Why is that a problem? Just call it mygetopt() and you have a getopt
with the same semantics on all the platforms supporting standard C.
The problems are caused by non-standard functions that cannot be
implemented in pure standard C, but this is not getopt's case.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
|
|
0
|
|
|
|
Reply
|
Dan.Pop (3615)
|
2/2/2004 5:38:01 PM
|
|
In article <bvm1tp$l2j$2@sunnews.cern.ch>, Dan.Pop@cern.ch says...
> In <pan.2004.01.30.22.29.24.686412@ig.com.br> =?iso-8859-1?q?Jos=E9_de_Paula?= <jose_de_paula@ig.com.br> writes:
>
> >Thank you, but the Source is with me, Luke! :-) The problem is that if I
> >want to write code that is portable across different operating systems
> >I'll have to include the wheel (i.e., an implementation of getopt()) with
> >it.
>
> Why is that a problem? Just call it mygetopt() and you have a getopt
> with the same semantics on all the platforms supporting standard C.
>
> The problems are caused by non-standard functions that cannot be
> implemented in pure standard C, but this is not getopt's case.
I've been doing that for a very long time, only I call it my_getopt().
It has worked on every platform i've tried, in strict conforming mode,
even Windows. :-)
--
Randy Howard
2reply remove FOOBAR
|
|
0
|
|
|
|
Reply
|
randyhoward (3272)
|
2/4/2004 11:33:54 AM
|
|
|
14 Replies
28 Views
(page loaded in 0.178 seconds)
|