473,421 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

code question

I have this code I would like to clean up.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
double x, y, a, b;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
a = strtod(argv[3], NULL);
b = strtod(argv[4], NULL);
if ((fp = fopen(argv[4], "a")) == NULL) {
puts("fopen error");
exit(-1);
}
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
if (fclose(fp) == EOF) {
puts("fclose error");
exit(-1);
}
return 0;
}

If the program is run with no arguments I get a seg fault. If it is run with
4, no problem. If it is run with less than four (that includes argv[0]) then
the program doesn't want to run right. How would I be able to use this
program with say one or two argvs ?

Bill
Sep 19 '08
83 2737
Default User said:
blargg wrote:
>In article <gb**********@registered.motzarella.org>,
Richard<rg****@gmail.comwrote:
Amazing stuff. Really. If he's a troll (and I really think he is)
then he's damn good.

And in a way, that makes him an excellent programmer of the wetware
of the people who just can't help not responding to him, time after
time after time.

I don't know and I don't much care. Bill is either a troll, or has been
"studying" C for at LEAST five years and made no more progress than
this. If he's not a troll, then I don't see any reason to think that
another five years or fifty years of CLC pouring out their advice will
make the slightest difference.

Replying to Bill is a waste of time one way or the other.
I agree that Bill Cunningham doesn't appear to gain any significant benefit
from the responses he gets. Whether this is through malice or incompetence
is really beside the point, and Hanlon's Razor applies.

But his questions do sometimes provoke discussions that are likely to be of
moderate interest to /other/ learners. If you think of a reply to one of
Bill Cunningham's articles not as a reply to /him/, but as a reply to the
points he has made, for general consumption, then it may perhaps seem
slightly less of a Sisyphean task.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 20 '08 #51
On Sat, 20 Sep 2008 00:50:59 -0400,
CBFalconer <cb********@yahoo.comwrote:
Bill Cunningham wrote:
>>
Someone pointed out to me Richard that an answer to a question I
raised about bitwise operators was on page 49 or kandr2. I read,
and re-read, and re-read page 49 about 3 times and strained to
keep my focus on reading and the text. The question was why is
the unary operator ~ shown like this:

x~34;
and not x=x~34;

Is the question indeed answered on that page?

Are you sure it is not "x ~= 34;"? Have you checked the published
errata sheet? I greatly doubt that K&R have suppressed the blanks
as you have.
I don't think Bill is saying that that question or that phrase appear on
that page. I think he's saying that that was his question, and someone
else told him that the answer to that could be found on page 49 of K&R2.

Martien
--
|
Martien Verbruggen | "In a world without fences,
| who needs Gates?"
|
Sep 20 '08 #52
Barry Schwarz wrote:
CBFalconer <cb********@yahoo.comwrote:
>Andrew Poelstra wrote:
>>Bill Cunningham <no****@nspam.invalidwrote:

Thanks Richard I know I can always count on you. I wondered
about argc and its uses.

In addition to argc, argv is also NULL terminated (IIRC!), so
you can loop through it without needed argc to find out when
to stop.

while (*argv) {
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.

Isn't that what the first line does?
My point, which I haven't bothered to check, is that on some
systems argv[0] doesn't point to an identifier of the program. If
that is the case, is argv[0] value NULL, or does it point to an
empty string? If it is a NULL, the scanning sequence won't work.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 20 '08 #53
CBFalconer <cb********@yahoo.comwrites:
Barry Schwarz wrote:
>CBFalconer <cb********@yahoo.comwrote:
>>Andrew Poelstra wrote:
Bill Cunningham <no****@nspam.invalidwrote:

Thanks Richard I know I can always count on you. I wondered
about argc and its uses.

In addition to argc, argv is also NULL terminated (IIRC!), so
you can loop through it without needed argc to find out when
to stop.

while (*argv) {
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.

Isn't that what the first line does?

My point, which I haven't bothered to check, is that on some
systems argv[0] doesn't point to an identifier of the program. If
What systems?

However IF the standard says that the first non existent argument slot
is NULL e.g

"myprog" then argv[1] is null then there really is not a problem if the
correct indices are used.

for(;*++argv;process(*argv));

Sep 20 '08 #54
CBFalconer <cb********@yahoo.comwrites:
Barry Schwarz wrote:
>CBFalconer <cb********@yahoo.comwrote:
>>Andrew Poelstra wrote:
Bill Cunningham <no****@nspam.invalidwrote:

Thanks Richard I know I can always count on you. I wondered
about argc and its uses.

In addition to argc, argv is also NULL terminated (IIRC!), so
you can loop through it without needed argc to find out when
to stop.

while (*argv) {
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.

Isn't that what the first line does?

My point, which I haven't bothered to check, is that on some
systems argv[0] doesn't point to an identifier of the program. If
that is the case, is argv[0] value NULL, or does it point to an
empty string? If it is a NULL, the scanning sequence won't work.
It might have been better to check. The code is fine. argv[0] can
only be NULL if argc == 0 in C90 and C99.

It is odd as it stands (because it processes a non-NULL argv[0] like
any other argument) but it is not wrong. It may, of course, be only a
fragment in which case it may not even be odd.

--
Ben.
Sep 21 '08 #55
On Sat, 20 Sep 2008 18:23:00 -0400, CBFalconer <cb********@yahoo.com>
wrote:
>Barry Schwarz wrote:
>CBFalconer <cb********@yahoo.comwrote:
>>Andrew Poelstra wrote:
Bill Cunningham <no****@nspam.invalidwrote:

Thanks Richard I know I can always count on you. I wondered
about argc and its uses.

In addition to argc, argv is also NULL terminated (IIRC!), so
you can loop through it without needed argc to find out when
to stop.

while (*argv) {
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.

Isn't that what the first line does?

My point, which I haven't bothered to check, is that on some
systems argv[0] doesn't point to an identifier of the program. If
that is the case, is argv[0] value NULL, or does it point to an
empty string? If it is a NULL, the scanning sequence won't work.
Since 5.1.2.2.1 guarantees argc >= 0 and argv[argc] == NULL, the
situation you are concerned about cannot occur.

--
Remove del for email
Sep 21 '08 #56
In article <iq******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
Default User said:
[...]
Replying to Bill is a waste of time one way or the other.

I agree that Bill Cunningham doesn't appear to gain any significant benefit
from the responses he gets. Whether this is through malice or incompetence
is really beside the point, and Hanlon's Razor applies.

But his questions do sometimes provoke discussions that are likely to be of
moderate interest to /other/ learners. If you think of a reply to one of
Bill Cunningham's articles not as a reply to /him/, but as a reply to the
points he has made, for general consumption, then it may perhaps seem
slightly less of a Sisyphean task.
Good point, but following it would mean answering his questions and not
replying further when he ignores the answer or mentions his copy of
"kandr2" from an alternate universe.
Sep 21 '08 #57
blargg <bl********@gishpuppy.comwrites:
In article <iq******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>Default User said:
[...]
Replying to Bill is a waste of time one way or the other.

I agree that Bill Cunningham doesn't appear to gain any significant benefit
from the responses he gets. Whether this is through malice or incompetence
is really beside the point, and Hanlon's Razor applies.

But his questions do sometimes provoke discussions that are likely to be of
moderate interest to /other/ learners. If you think of a reply to one of
Bill Cunningham's articles not as a reply to /him/, but as a reply to the
points he has made, for general consumption, then it may perhaps seem
slightly less of a Sisyphean task.

Good point, but following it would mean answering his questions and not
replying further when he ignores the answer or mentions his copy of
"kandr2" from an alternate universe.
Actually its not a good point at all. Primarily because Bill is yet to
ask a half decent question. He rarely if ever seems to know what he
wants leading to a rush of good Samaritans answering the question they
want it to be rather than the one he, supposedly, meant it to be.
Sep 21 '08 #58
Richard Heathfield wrote:
Default User said:
Replying to Bill is a waste of time one way or the other.

I agree that Bill Cunningham doesn't appear to gain any significant
benefit from the responses he gets. Whether this is through malice or
incompetence is really beside the point, and Hanlon's Razor applies.

But his questions do sometimes provoke discussions that are likely to
be of moderate interest to other learners. If you think of a reply to
one of Bill Cunningham's articles not as a reply to him, but as a
reply to the points he has made, for general consumption, then it may
perhaps seem slightly less of a Sisyphean task.
I'll freely admit that I still read through the threads, even though I
have Bill killfiled. Indeed, there's sometimes a useful nugget. Each
will have to make a decision on how to approach it.


Brian
Sep 21 '08 #59
CBFalconer said:
Barry Schwarz wrote:
>CBFalconer <cb********@yahoo.comwrote:
>>Andrew Poelstra wrote:
<snip>
>>>>
while (*argv) {
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.

Isn't that what the first line does?

My point, which I haven't bothered to check, is that on some
systems argv[0] doesn't point to an identifier of the program. If
that is the case, is argv[0] value NULL, or does it point to an
empty string? If it is a NULL, the scanning sequence won't work.
argv[0] is guaranteed *either* to represent the program name or to be NULL
(and argc to be 0), and the scanning sequence works fine whichever of
these is the case.

In future, please bother to check.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 21 '08 #60
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
argv[0] is guaranteed *either* to represent the program name or to be NULL
(and argc to be 0), and the scanning sequence works fine whichever of
these is the case.
[...]

argv[0] is also allowed to point to an empty string if the program
name is not available. The standard's exact wording (C99 5.1.2.2.1p2)
is:

If the value of argc is greater than zero, the string pointed to
by argv[0] represents the _program name_; argv[0][0] shall be the
null character if the program name is not available from the host
environment.

But the requirement that the string pointed to by argv[0][0]
"represents the program name" is largely unenforced and unenforceable.
Under POSIX, for example, the string is whatever the invoking program
wants it to be. Though I suppose you could define "program name" in
such a way as to make this consistent. In fact, since "program name"
is in italics in the quoted paragraph, that supposedly *is* the
definition of the phrase -- though I'm not convinced it's really a
definition at all. It would make as much sense, and be more
consistent, for the standard to say that the string *is* the program
name.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 21 '08 #61
Barry Schwarz wrote:
CBFalconer <cb********@yahoo.comwrote:
.... snip ...
>
>My point, which I haven't bothered to check, is that on some
systems argv[0] doesn't point to an identifier of the program. If
that is the case, is argv[0] value NULL, or does it point to an
empty string? If it is a NULL, the scanning sequence won't work.

Since 5.1.2.2.1 guarantees argc >= 0 and argv[argc] == NULL, the
situation you are concerned about cannot occur.
I wasn't talking about argv[argc]. I mentioned argv[0].
Apparently there is no worry.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 21 '08 #62
Richard Heathfield wrote:
CBFalconer said:
.... snip ...
>
>My point, which I haven't bothered to check, is that on some
systems argv[0] doesn't point to an identifier of the program. If
that is the case, is argv[0] value NULL, or does it point to an
empty string? If it is a NULL, the scanning sequence won't work.

argv[0] is guaranteed *either* to represent the program name or to
be NULL (and argc to be 0), and the scanning sequence works fine
whichever of these is the case.

In future, please bother to check.
Don't be so silly. I'm not worried about using it. I simply
raised something for others, who might be worried, to check.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 21 '08 #63
blargg wrote:
Richard Heathfield <rj*@see.sig.invalidwrote:
>Default User said:
[...]
>>Replying to Bill is a waste of time one way or the other.

I agree that Bill Cunningham doesn't appear to gain any
significant benefit from the responses he gets. Whether this is
through malice or incompetence is really beside the point, and
Hanlon's Razor applies.

But his questions do sometimes provoke discussions that are
likely to be of moderate interest to /other/ learners. If you
think of a reply to one of Bill Cunningham's articles not as a
reply to /him/, but as a reply to the points he has made, for
general consumption, then it may perhaps seem slightly less of
a Sisyphean task.

Good point, but following it would mean answering his questions
and not replying further when he ignores the answer or mentions
his copy of "kandr2" from an alternate universe.
Do whatever you wish. However my judgement is that there is
neither malice nor incompetence involved, the man has a mental
disability. In fact, I admire his persistence, and have noted
slight progress over the years. One advantage of Usenet is that
you are quite free to ignore him.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 21 '08 #64
CBFalconer said:
Richard Heathfield wrote:
>CBFalconer said:
... snip ...
>>
>>My point, which I haven't bothered to check, is that on some
systems argv[0] doesn't point to an identifier of the program. If
that is the case, is argv[0] value NULL, or does it point to an
empty string? If it is a NULL, the scanning sequence won't work.

argv[0] is guaranteed *either* to represent the program name or to
be NULL (and argc to be 0), and the scanning sequence works fine
whichever of these is the case.

In future, please bother to check.

Don't be so silly. I'm not worried about using it. I simply
raised something for others, who might be worried, to check.
Don't be so silly. The only person worried about it was you. Nobody else
even suggested that the "scanning sequence won't work" if argv[0] is NULL,
for the excellent reason that it works just fine.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 21 '08 #65
Keith Thompson said:
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
>argv[0] is guaranteed *either* to represent the program name or to be
NULL (and argc to be 0), and the scanning sequence works fine whichever
of these is the case.
[...]

argv[0] is also allowed to point to an empty string if the program
name is not available.
In which case the "scanning sequence" still works just fine.
But the requirement that the string pointed to by argv[0][0]
ITYM argv[0].

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 21 '08 #66
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
>Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
>>argv[0] is guaranteed *either* to represent the program name or to be
NULL (and argc to be 0), and the scanning sequence works fine whichever
of these is the case.
[...]

argv[0] is also allowed to point to an empty string if the program
name is not available.

In which case the "scanning sequence" still works just fine.
Yes.
>But the requirement that the string pointed to by argv[0][0]

ITYM argv[0].
D'oh!

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 21 '08 #67
"Default User" <de***********@yahoo.comwrites:
Richard Heathfield wrote:
>Default User said:

Replying to Bill is a waste of time one way or the other.

I agree that Bill Cunningham doesn't appear to gain any significant
benefit from the responses he gets. Whether this is through malice or
incompetence is really beside the point, and Hanlon's Razor applies.

But his questions do sometimes provoke discussions that are likely to
be of moderate interest to other learners. If you think of a reply to
one of Bill Cunningham's articles not as a reply to him, but as a
reply to the points he has made, for general consumption, then it may
perhaps seem slightly less of a Sisyphean task.

I'll freely admit that I still read through the threads, even though I
have Bill killfiled. Indeed, there's sometimes a useful nugget. Each
will have to make a decision on how to approach it.
That's nice of you Brian. I'm sure we all appreciate the "go ahead" from
such a c.l.c luminary as yourself.
Brian
You need to try and get a decent news reader. If you killfile Bill
surely you have no desire to read his questions or the replies? Or are
you hunting for "off topicality"?

Sep 21 '08 #68
On Sep 20, 5:03*am, Richard Heathfield <r...@see.sig.invalidwrote:
Bill Cunningham said:
* * I have this code I would like to clean up.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
* * double x, y, a, b;
* * FILE *fp;
* * x = strtod(argv[1], NULL);
* * y = strtod(argv[2], NULL);
* * a = strtod(argv[3], NULL);
* * b = strtod(argv[4], NULL);
* * if ((fp = fopen(argv[4], "a")) == NULL) {
* * * * puts("fopen error");
* * * * exit(-1);
* * }
* * fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
* * if (fclose(fp) == EOF) {
* * * * puts("fclose error");
* * * * exit(-1);
* * }
* * return 0;
}
If the program is run with no arguments I get a seg fault. If it is run
with 4, no problem. If it is run with less than four (that includes
argv[0]) then the program doesn't want to run right. How would I be able
to use this program with say one or two argvs ?

#include <stdio.h>
#include <stdlib.h>

#define DEFAULT_FILE_NAME "foo.bar"

int main(int argc, char *argv[])
{
* * double x = 0.0, y = 0.0, a = 0.0, b = 0.0;
* * const char *filename = DEFAULT_FILE_NAME;

* * FILE *fp = NULL;
* * if(argc 1)
* * {
* * * x = strtod(argv[1], NULL);
* * }
* * if(argc 2)
* * {
* * * y = strtod(argv[2], NULL);
* * }
* * if(argc 3)
* * {
* * * a = strtod(argv[3], NULL);
* * }
* * if(argc 4)
* * {
* * * b = strtod(argv[4], NULL);
* * * filename = argv[4];
* * }
* * if ((fp = fopen(filename, "a")) == NULL) {
* * * * puts("fopen error");
* * * * exit(EXIT_FAILURE);
* * }
* * fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
* * if (fclose(fp) == EOF) {
* * * * puts("fclose error");
* * * * exit(EXIT_FAILURE);
* * }
* * return 0;

}
The error check upon strtod is missed four times freely.
Sep 22 '08 #69
lovecreatesbea...@gmail.com said:

<snip>
The error check upon strtod is missed four times freely.
Then insert it, dear lo***************@gmail.com, dear
lo***************@gmail.com, dear lo***************@gmail.com
Then insert it, dear lo***************@gmail.com, dear
lo***************@gmail.com, insert it.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 22 '08 #70
On Sep 22, 3:56*pm, Richard Heathfield <r...@see.sig.invalidwrote:
lovecreatesbea...@gmail.com said:

<snip>
The error check upon strtod is missed four times freely.

Then insert it
Yeah, how about this:

$ cat a.c
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main (int argc, char *argv[])
{
double d;
char *end;

for (; *++argv; ){
errno = 0;
d = strtod(*argv, &end);
if (errno){
perror(*argv);
continue;
}
if (d == 0 && *argv == end){
fprintf(stderr, "%s: Cant be converted\n",
*argv);
continue;
}
fprintf(stdout, "%f\n", d);
}
return EXIT_SUCCESS;
}
$
$ make && ./a.out 11
gcc -Wall -W -g -pedantic -ansi -c -o a.o a.c
a.c:5: warning: unused parameter ‘argc’
gcc a.o -o a.out
11.000000
$ make && ./a.out aa
make: `a.out' is up to date.
aa: Cant be converted
$ make && ./a.out
99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 9999999999999999999999999999.99
make: `a.out' is up to date.
99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 9999999999999999999999999999.99:
Numerical result out of range
$
Sep 22 '08 #71
lovecreatesbea...@gmail.com said:
On Sep 22, 3:56 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>lovecreatesbea...@gmail.com said:

<snip>
The error check upon strtod is missed four times freely.

Then insert it

Yeah, how about this:

$ cat a.c
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main (int argc, char *argv[])
{
double d;
char *end;

for (; *++argv; ){
errno = 0;
d = strtod(*argv, &end);
if (errno){
perror(*argv);
continue;
}
if (d == 0 && *argv == end){
fprintf(stderr, "%s: Cant be converted\n",
*argv);
continue;
}
fprintf(stdout, "%f\n", d);
}
return EXIT_SUCCESS;
}
Unfortunately, this destroys the value of each argument (except the last)
without storing it safely for further usage. This is known as "throwing
the baby out with the bathwater".

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 22 '08 #72
On Mon, 22 Sep 2008 03:20:09 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote:
>On Sep 22, 3:56*pm, Richard Heathfield <r...@see.sig.invalidwrote:
>lovecreatesbea...@gmail.com said:

<snip>
The error check upon strtod is missed four times freely.

Then insert it

Yeah, how about this:

$ cat a.c
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main (int argc, char *argv[])
{
double d;
char *end;

for (; *++argv; ){
errno = 0;
d = strtod(*argv, &end);
if (errno){
perror(*argv);
continue;
}
if (d == 0 && *argv == end){
If *argv == end, does it matter what d is?

This doesn't handle the situation where the input is "12xyz". You
really want to check the *end == '\0'.
fprintf(stderr, "%s: Cant be converted\n",
*argv);
continue;
}
fprintf(stdout, "%f\n", d);
}
return EXIT_SUCCESS;
}
--
Remove del for email
Sep 23 '08 #73
On Sep 23, 10:18*am, Barry Schwarz <schwa...@dqel.comwrote:
On Mon, 22 Sep 2008 03:20:09 -0700 (PDT),

"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote:
On Sep 22, 3:56*pm, Richard Heathfield <r...@see.sig.invalidwrote:
lovecreatesbea...@gmail.com said:
<snip>
The error check upon strtod is missed four times freely.
Then insert it
Yeah, how about this:
$ cat a.c
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main (int argc, char *argv[])
{
* * * *double d;
* * * *char *end;
* * * *for (; *++argv; ){
* * * * * * * *errno = 0;
* * * * * * * *d = strtod(*argv, &end);
* * * * * * * *if (errno){
* * * * * * * * * * * *perror(*argv);
* * * * * * * * * * * *continue;
* * * * * * * *}
* * * * * * * *if (d == 0 && *argv == end){

If *argv == end, does it matter what d is?

This doesn't handle the situation where the input is "12xyz". *You
really want to check the *end == '\0'.
Thank you for also pointed out this in my other post before. I add an
additional check same as before to let input like "12 " pass
through.
>
* * * * * * * * * * * *fprintf(stderr, "%s: Cant be converted\n",
*argv);
* * * * * * * * * * * *continue;
* * * * * * * *}
* * * * * * * *fprintf(stdout, "%f\n", d);
* * * *}
* * * *return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[])
{
double d;
char **a = argv, *e;

while (*++a){
errno = 0;
d = strtod(*a, &e);
if (errno){
perror(*a);
continue;
}
if (d == 0 && *a == e){
fprintf(stderr, "%s: Cant be converted\n",
*a);
continue;
}
while (*e){
if (!isspace(*e))
break;
e++;
}
if (!*e)
fprintf(stdout, "%f, %s\n\n", d, *a);
}
return EXIT_SUCCESS;
}
Sep 23 '08 #74
On Sep 23, 11:57*am, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
On Sep 23, 10:18*am, Barry Schwarz <schwa...@dqel.comwrote:


On Mon, 22 Sep 2008 03:20:09 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote:
>On Sep 22, 3:56*pm, Richard Heathfield <r...@see.sig.invalidwrote:
>lovecreatesbea...@gmail.com said:
><snip>
The error check upon strtod is missed four times freely.
>Then insert it
>Yeah, how about this:
>$ cat a.c
>#include <stdlib.h>
>#include <stdio.h>
>#include <errno.h>
>int main (int argc, char *argv[])
>{
* * * *double d;
* * * *char *end;
* * * *for (; *++argv; ){
* * * * * * * *errno = 0;
* * * * * * * *d = strtod(*argv, &end);
* * * * * * * *if (errno){
* * * * * * * * * * * *perror(*argv);
* * * * * * * * * * * *continue;
* * * * * * * *}
* * * * * * * *if (d == 0 && *argv == end){
If *argv == end, does it matter what d is?
This doesn't handle the situation where the input is "12xyz". *You
really want to check the *end == '\0'.

Thank you for also pointed out this in my other post before. I add an
additional check same as before to let input like "12 * " pass
through.
* * * * * * * * * * * *fprintf(stderr, "%s: Cant be converted\n",
>*argv);
* * * * * * * * * * * *continue;
* * * * * * * *}
* * * * * * * *fprintf(stdout, "%f\n", d);
* * * *}
* * * *return EXIT_SUCCESS;
>}

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[])
{
* * * * double d;
* * * * char **a = argv, *e;

* * * * while (*++a){
* * * * * * * * errno = 0;
* * * * * * * * d = strtod(*a, &e);
* * * * * * * * if (errno){
* * * * * * * * * * * * perror(*a);
* * * * * * * * * * * * continue;
* * * * * * * * }
* * * * * * * * if (d == 0 && *a == e){
* * * * * * * * * * * * fprintf(stderr, "%s: Cantbe converted\n",
*a);
* * * * * * * * * * * * continue;
* * * * * * * * }
* * * * * * * * while (*e){
* * * * * * * * * * * * if (!isspace(*e))
* * * * * * * * * * * * * * * * break;
* * * * * * * * * * * * e++;
* * * * * * * * }
* * * * * * * * if (!*e)
* * * * * * * * * * * * fprintf(stdout, "%f, %s\n\n", d, *a);
* * * * }
* * * * return EXIT_SUCCESS;

}
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[])
{
double d;
char **a = argv, *e;

while (*++a){
errno = 0;
d = strtod(*a, &e);
if (errno){
perror(*a);
continue;
}
if (d == 0 && *a == e){
fprintf(stderr, "%s: Cant be converted\n",
*a);
continue;
}
while (*e)
if (!isspace(*e++))
break;
if (*e){
fprintf(stderr, "%s: Invalid format\n", *a);
continue;
}
fprintf(stdout, "%f, %s\n\n", d, *a);
}
return EXIT_SUCCESS;
}
Sep 23 '08 #75
On Sep 23, 1:38 pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
On Sep 23, 11:57 am, "lovecreatesbea...@gmail.com"
....

>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[])
{
double d;
char **a = argv, *e;

while (*++a){
errno = 0;
d = strtod(*a, &e);
if (errno){
perror(*a);
continue;
}
if (d == 0 && *a == e){
fprintf(stderr, "%s: Cant be converted\n",
*a);
continue;
}
while (*e)
if (!isspace(*e++))
break;
Sorry, the while loop needs to be changed back.

while (*e){
if (!isspace(*e))
break;
e++;
}
if (*e){
fprintf(stderr, "%s: Invalid format\n", *a);
continue;
}
fprintf(stdout, "%f, %s\n\n", d, *a);
}
return EXIT_SUCCESS;

}
Sep 23 '08 #76
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
<snip>
Sorry, the while loop needs to be changed back.

while (*e){
if (!isspace(*e))
break;
e++;
}
A detail (I've not looked at the rest): C's logical operators allw
this to be safely written with no if and no break:

while (*e && isspace(*e))
e++;

--
Ben.
Sep 23 '08 #77
On Sep 23, 10:50*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrites:

<snip>
Sorry, the while loop needs to be changed back.
* * * * * * * * * while (*e){
* * * * * * * * * * * * * if (!isspace(*e))
* * * * * * * * * * * * * * * * * break;
* * * * * * * * * * * * * e++;
* * * * * * * * * }

A detail (I've not looked at the rest): *C's logical operators allw
this to be safely written with no if and no break:

* while (*e && isspace(*e))
* * * e++;
Yes, it's better.

- thanks
Sep 23 '08 #78
On Mon, 22 Sep 2008 22:38:35 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote:

snip 100 lines of obsolete code

Please trim your posts.
>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[])
{
double d;
char **a = argv, *e;

while (*++a){
errno = 0;
d = strtod(*a, &e);
if (errno){
perror(*a);
continue;
}
if (d == 0 && *a == e){
The expression d==0 is still unnecessary.
fprintf(stderr, "%s: Cant be converted\n",
*a);
continue;
}
while (*e)
if (!isspace(*e++))
break;
if (*e){
fprintf(stderr, "%s: Invalid format\n", *a);
continue;
}
Why do you consider input of 12xy to be "better" (or even different)
than input of xy12?
fprintf(stdout, "%f, %s\n\n", d, *a);
}
return EXIT_SUCCESS;
}
--
Remove del for email
Sep 24 '08 #79
On Sep 24, 9:50 am, Barry Schwarz <schwa...@dqel.comwrote:
On Mon, 22 Sep 2008 22:38:35 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main (int argc, char *argv[])
{
double d;
char **a = argv, *e;
while (*++a){
errno = 0;
d = strtod(*a, &e);
if (errno){
perror(*a);
continue;
}
if (d == 0 && *a == e){

The expression d==0 is still unnecessary.
The documents (the Standard, manpages) say:

If no conversion is performed, zero is returned and the value of
nptr
is stored in the location referenced by endptr.

So I checked those two.
>
fprintf(stderr, "%s: Cant be converted\n",
*a);
continue;
}
while (*e)
if (!isspace(*e++))
break;
if (*e){
fprintf(stderr, "%s: Invalid format\n", *a);
continue;
}

Why do you consider input of 12xy to be "better" (or even different)
than input of xy12?
But my code issues error messages on both inputs already.

strtod can accept "12xyz" and "12 xyz" and return (double)12. Can I
also accept these inputs in some of my code?
>
fprintf(stdout, "%f, %s\n\n", d, *a);
}
return EXIT_SUCCESS;
}
Sep 24 '08 #80
Ben Bacarisse wrote:
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
<snip>
>Sorry, the while loop needs to be changed back.

while (*e){
if (!isspace(*e))
break;
e++;
}

A detail (I've not looked at the rest): C's logical operators allw
this to be safely written with no if and no break:

while (*e && isspace(*e))
e++;
Assuming isspace(0) is false:

while (isspace( *e )) e += 1;

(layout, braces, and increment-style to taste.)

[I was /sure/ that isspace(0) was false, but a quick skim doesn't
find me enough answers to be compelling. Anyone else have the
necessary clues?]

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Sep 24 '08 #81
Chris Dollin <ch**********@hp.comwrites:
Ben Bacarisse wrote:
>"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
<snip>
>>Sorry, the while loop needs to be changed back.

while (*e){
if (!isspace(*e))
break;
e++;
}

A detail (I've not looked at the rest): C's logical operators allw
this to be safely written with no if and no break:

while (*e && isspace(*e))
e++;

Assuming isspace(0) is false:

while (isspace( *e )) e += 1;

(layout, braces, and increment-style to taste.)

[I was /sure/ that isspace(0) was false, but a quick skim doesn't
find me enough answers to be compelling. Anyone else have the
necessary clues?]
I left it as a general pattern rather than going the whole hog.

I am pretty sure that isspace(0) is false because, to be true, it
would have to be "one of a locale-specific set of characters for which
isalnum is false" and I don't think any of these locale-specific
characters can also be in the basic execution character set (which
includes the null character).

--
Ben.
Sep 24 '08 #82
On Tue, 23 Sep 2008 19:37:50 -0700 (PDT),
"lo***************@gmail.com" <lo***************@gmail.comwrote:
>On Sep 24, 9:50 am, Barry Schwarz <schwa...@dqel.comwrote:
>On Mon, 22 Sep 2008 22:38:35 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote:
>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
>int main (int argc, char *argv[])
{
double d;
char **a = argv, *e;
while (*++a){
errno = 0;
d = strtod(*a, &e);
if (errno){
perror(*a);
continue;
}
if (d == 0 && *a == e){

The expression d==0 is still unnecessary.

The documents (the Standard, manpages) say:

If no conversion is performed, zero is returned and the value of
nptr
is stored in the location referenced by endptr.

So I checked those two.
If *a ==e, d must be 0. If d !=0, then *a == e must be false also.
The fact that zero is returned is just a simple way to guarantee that
the behavior is not undefined (as happens with ato.. functions).

Put another way, if d==0, you still must check *a and e. If d == 0 is
falser, then *a == e is also guaranteed to be false. The check on d
provides no additional information.
>
>>
fprintf(stderr, "%s: Cant be converted\n",
*a);
continue;
}
while (*e)
if (!isspace(*e++))
break;
if (*e){
fprintf(stderr, "%s: Invalid format\n", *a);
continue;
}

Why do you consider input of 12xy to be "better" (or even different)
than input of xy12?

But my code issues error messages on both inputs already.

strtod can accept "12xyz" and "12 xyz" and return (double)12. Can I
also accept these inputs in some of my code?
You can do whatever you want with 12xy but, in my opinion, I don't
see why you would want to accept it as 12.0. A malformed input is
malformed whether the error is on the first or the last character or
any in between.
>
>>
fprintf(stdout, "%f, %s\n\n", d, *a);
}
return EXIT_SUCCESS;
}
--
Remove del for email
Sep 25 '08 #83
Barry Schwarz wrote:
<lo***************@gmail.comwrote:
.... snip ...
>
>strtod can accept "12xyz" and "12 xyz" and return (double)12.
Can I also accept these inputs in some of my code?

You can do whatever you want with 12xy but, in my opinion, I
don't see why you would want to accept it as 12.0. A malformed
input is malformed whether the error is on the first or the
last character or any in between.
I disagree. These consist of the number '12' terminated by one of:

'\n',
' ',
'x'.

and you find out which by examining the value of *e. Then you can
decide between acceptance and rejection.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 25 '08 #84

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mohammed Mazid | last post by:
Hi folks! Can anyone please help me with this? I am developing a Quiz program but I am stuck with "multiple answers". Basically I need some sort of code that would select multiple answers...
70
by: grün | last post by:
The MSDN techdocs are somewhat limited on this and I wanted more information. Is there any resource that says definitively which is faster /O2 or /Ox and by how much?
19
by: Rhek | last post by:
Hello, I would like to apologize for double posting this question because I posted this same question in what looks like the VB 6 newgroups and not the .Net newsgroup... Here goes: The code...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
8
by: Andy B | last post by:
Before I do a no no on a newsgroup, I need to ask a question: What is the max number of lines of code you can/should post here before it gets too long?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.