473,324 Members | 2,166 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,324 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 #1
83 2728
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;
}

--
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 19 '08 #2
Bill Cunningham wrote:
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 ?
Your requirements are incomplete.

What should happen if there are less than four arguments? Should a file
be opened? Should an error message be output?

Without these details, there a number of conflicting possible solutions.

--
Ian Collins.
Sep 19 '08 #3

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:wL*********************@bt.com...
#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;
}
Thanks Richard I know I can always count on you. I wondered about argc
and its uses.

Bill
Sep 19 '08 #4
Bill Cunningham wrote:
I have this code I would like to clean up.
Well, it certainly needs cleaning up, so you have got this part right.
See below.
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 ?
There are many ways to hand differing numbers of arguments to a program.
Here is one way to start on yours, except that making your filename
argv[4] makes the exercise pointless.

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

int main(int argc, char *argv[])
{
double x = 0, y = 0, a = 0, b = 0;
FILE *fp;
switch (argc) {
case 5:
b = strtod(argv[4], NULL);
case 4:
a = strtod(argv[3], NULL);
case 3:
y = strtod(argv[2], NULL);
case 2:
x = strtod(argv[1], NULL);
case 1:
break;
default:
fputs("I don't know how to handle that many arguments.\n",
stderr);
exit(EXIT_FAILURE);
}
if (argc < 5) {
fprintf(stderr, "%s",
"I just wasted some time assigning values to x, y, a, &"
"b\nsince the fourth argument (argv[4]) is needed for a"
" filename\nand you didn't supply it.\n");
exit(EXIT_FAILURE);
}
if ((fp = fopen(argv[4] /* this is nonsense, of course, if
argv < 5 */ , "a")) == NULL) {
fputs("fopen error\n", stderr);
exit(EXIT_FAILURE); /* fixed non-portable 'exit(-1)' */
}
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
if (fclose(fp) == EOF) {
fputs("fclose error\n", stderr);
exit(EXIT_FAILURE); /* fixed non-portable 'exit(-1)' */
}
return 0;
}
Sep 19 '08 #5

"Martin Ambuhl" <ma*****@earthlink.netwrote in message
news:gb**********@registered.motzarella.org...
>
Well, it certainly needs cleaning up, so you have got this part right. See
below.
There are many ways to hand differing numbers of arguments to a program.
Here is one way to start on yours, except that making your filename
argv[4] makes the exercise pointless.

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

int main(int argc, char *argv[])
{
double x = 0, y = 0, a = 0, b = 0;
FILE *fp;
Great. Switch. I've never used it before.
switch (argc) {
case 5:
b = strtod(argv[4], NULL);
case 4:
a = strtod(argv[3], NULL);
case 3:
y = strtod(argv[2], NULL);
case 2:
x = strtod(argv[1], NULL);
case 1:
break;
default:
fputs("I don't know how to handle that many arguments.\n",
stderr);
What's going on here? Is the default for more than the number of argv's
that the program calls for ? Why is there a break and not just default?
exit(EXIT_FAILURE);
}
if (argc < 5) {
fprintf(stderr, "%s",
"I just wasted some time assigning values to x, y, a, &"
"b\nsince the fourth argument (argv[4]) is needed for a"
" filename\nand you didn't supply it.\n");
exit(EXIT_FAILURE);
}
if ((fp = fopen(argv[4] /* this is nonsense, of course, if
argv < 5 */ , "a")) == NULL) {
fputs("fopen error\n", stderr);
exit(EXIT_FAILURE); /* fixed non-portable 'exit(-1)' */
}
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
if (fclose(fp) == EOF) {
fputs("fclose error\n", stderr);
exit(EXIT_FAILURE); /* fixed non-portable 'exit(-1)' */
}
return 0;
}
If exit(-1) is non -portable. Which I was afraid of what about exit(1)?
Or does the standard only allow exit(EXIT_FAILURE);

Bill
Sep 19 '08 #6

"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
Your requirements are incomplete.

What should happen if there are less than four arguments? Should a file
be opened? Should an error message be output?

Without these details, there a number of conflicting possible solutions.
Yes if there are less than four arguments a file should be opened. There
must be a way to only write the fprintf stuff once though.

Bill
Sep 19 '08 #7
Bill Cunningham wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
>Your requirements are incomplete.

What should happen if there are less than four arguments? Should a file
be opened? Should an error message be output?

Without these details, there a number of conflicting possible solutions.
Yes if there are less than four arguments a file should be opened. There
must be a way to only write the fprintf stuff once though.
The how about

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

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;
}

FILE *fp = fopen(argv[n-1], "a");

if ( fp == NULL ) {
puts("fopen error");
exit(EXIT_FAILURE);
}

fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", data[0], data[1], data[2],
data[3]);

if (fclose(fp) == EOF ) {
puts("fclose error");
exit(EXIT_FAILURE);
}

return 0;
}

--
Ian Collins.
Sep 19 '08 #8
On 2008-09-19, 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;
}

--
Andrew Poelstra ap*******@wpsoftware.net
That was a joke. Jokes in mathematics, are sometimes not funny.
-Veselin Jungic
Sep 19 '08 #9
Andrew Poelstra said:

<snip>
In addition to argc, argv is also NULL terminated (IIRC!),
YRC. argv[argc] is guaranteed to be a null pointer (even if argc is 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 19 '08 #10
"Bill Cunningham" <no****@nspam.invalidwrites:
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
Did you try it with a debugger? No? Then give up. Not that you need one
to spot the error. 10/10 for persistence. O/10 for originality. These
posts are getting a tad tiresome.
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 #11
"Bill Cunningham" <no****@nspam.invalidwrites:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:wL*********************@bt.com...
>#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;
}

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

Bill
You wondered about argc?

You never thought to

(a) see where it crashed using a debugger?
(b) examine the values of argc and argv in a debugger?

or

(c) read up about them in your copy of K&R2? Heathfield must really need
some soul cleansing if he responded to you with a total clean up with
no admonitions.
Sep 19 '08 #12

"Richard" <rg****@gmail.comwrote in message
news:gb*********@registered.motzarella.org...
Did you try it with a debugger? No? Then give up. Not that you need one
to spot the error. 10/10 for persistence. O/10 for originality. These
posts are getting a tad tiresome.
So are yours Richard Good God so are yours.

Have a nice life..

Sep 20 '08 #13
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?

Bill
Sep 20 '08 #14
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
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);
So the fourth command line argument should be a number.
if ((fp = fopen(argv[4], "a")) == NULL) {
At the same time, it should be a file name.

That doesn't strike you as slightly odd?

--
Remove del for email
Sep 20 '08 #15
Barry Schwarz wrote:
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
> 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);

So the fourth command line argument should be a number.
> if ((fp = fopen(argv[4], "a")) == NULL) {

At the same time, it should be a file name.

That doesn't strike you as slightly odd?
Most things "Bill" posts are at least slightly odd...

--
Ian Collins.
Sep 20 '08 #16

"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
The how about

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

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;
[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?

Bill
Sep 20 '08 #17
"Bill Cunningham" <no****@nspam.invalidwrites:
"Richard" <rg****@gmail.comwrote in message
news:gb*********@registered.motzarella.org...
>Did you try it with a debugger? No? Then give up. Not that you need one
to spot the error. 10/10 for persistence. O/10 for originality. These
posts are getting a tad tiresome.

So are yours Richard Good God so are yours.

Have a nice life..
So, did you use the debugger to show you were it was segment faulting?
No? Then give up. You are totally useless as a programmer and will never
make it. Why? Because you refuse to use the tools there to help you do a
job I offer this advise to you to save you a lot of hurt and pain in the
future. If it's any consolation I could never be a high wire trapeze
artist no matter how much I practised.

And while I am at it, please stop spamming the application development
groups telling them you are trying to write a printer driver when you
could not even tell them the type of printer you are using.

Sep 20 '08 #18
Barry Schwarz <sc******@dqel.comwrites:
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
> 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);

So the fourth command line argument should be a number.
> if ((fp = fopen(argv[4], "a")) == NULL) {

At the same time, it should be a file name.

That doesn't strike you as slightly odd?
Not it doesn't because he never reads his own code. He refused to use a
debugger or debugging methods to see his data in action. In short he is
either a troll or the worst programmer of all time. And I mean *ALL*
time.
Sep 20 '08 #19
Bill Cunningham wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
>The how about

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

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;

[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?
What's argv[1]?

--
Ian Collins.
Sep 20 '08 #20
kandr2 is about the toughest reading there is on C. I'll try again in a
couple of days when my concentration can be focused.

Bill
Sep 20 '08 #21
"Bill Cunningham" <no****@nspam.invalidwrites:
"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
>The how about

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

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;

[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?

Bill
To confuse you I assume. Its not a very C way of doing it IMO. Not
tested for ISO C correctness or whether it even works but this is much
clearer if you (and you must as a c programmer) understand for
loops. You should get the idea.

for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);
Sep 20 '08 #22
Richard<rg****@gmail.comwrites:
"Bill Cunningham" <no****@nspam.invalidwrites:
>"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
>>The how about

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

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;

[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?

Bill

To confuse you I assume. Its not a very C way of doing it IMO. Not
tested for ISO C correctness or whether it even works but this is much
clearer if you (and you must as a c programmer) understand for
loops. You should get the idea.

for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);
ps use a debugger. And tell me my error from posting too quickly
there. Hint : look at what is in argv and what you are interested in.
Sep 20 '08 #23
Richard wrote:
"Bill Cunningham" <no****@nspam.invalidwrites:
>"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
>>The how about

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

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;
[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?

To confuse you I assume.
Nope, I just posted before refactoring!

--
Ian Collins.
Sep 20 '08 #24
Ian Collins <ia******@hotmail.comwrites:
Richard wrote:
>"Bill Cunningham" <no****@nspam.invalidwrites:
>>"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
The how about

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

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;
[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?

To confuse you I assume.

Nope, I just posted before refactoring!
I apologise that I didn't get the tone right there. I wasn't being
insulting to you - I was more hinting that anything could confuse Bill.
Sep 20 '08 #25
Richard wrote:
>
for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);
At lest my "non C" way of doing it didn't have the bug you have...

I won't spoil the reader's fun by saying what it is.

--
Ian Collins.
Sep 20 '08 #26
Richard wrote:
Ian Collins <ia******@hotmail.comwrites:
>Richard wrote:
>>>>
To confuse you I assume.
Nope, I just posted before refactoring!

I apologise that I didn't get the tone right there. I wasn't being
insulting to you - I was more hinting that anything could confuse Bill.
I can't argue with that!

--
Ian Collins.
Sep 20 '08 #27
Richard<rg****@gmail.comwrites:
"Bill Cunningham" <no****@nspam.invalidwrites:
>"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
<snip>
>> unsigned n = 1;
while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;

[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?
<snip>
To confuse you I assume. Its not a very C way of doing it IMO. Not
tested for ISO C correctness or whether it even works but this is much
clearer if you (and you must as a c programmer) understand for
loops. You should get the idea.

for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);
Great disclaimer! "This is better (but it may be wrong)". Yes it is
(wrong). Here is the flaw with using a debugger. It is easier to
think about this code than to paste this into a main function and
debug it.

--
Ben.
Sep 20 '08 #28
Richard wrote:
And while I am at it, please stop spamming the application development
groups telling them you are trying to write a printer driver when you
could not even tell them the type of printer you are using.
[Testing out THunderbird]

Go back a read the thread for what it says. We were conversing about my
printer make and model. I don't need to defend what's posted.
>
Sep 20 '08 #29

"Barry Schwarz" <sc******@dqel.comwrote in message
news:7f********************************@4ax.com...
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
> if ((fp = fopen(argv[4], "a")) == NULL) {

At the same time, it should be a file name.

That doesn't strike you as slightly odd?
A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you. Things like this for example.
That why clc is needed.

Bill
Sep 20 '08 #30
Bill Cunningham wrote:
"Barry Schwarz" <sc******@dqel.comwrote in message
news:7f********************************@4ax.com...
>On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
>> if ((fp = fopen(argv[4], "a")) == NULL) {
At the same time, it should be a file name.

That doesn't strike you as slightly odd?
A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you.
Like common sense?

--
Ian Collins.
Sep 20 '08 #31
Ben Bacarisse <be********@bsb.me.ukwrites:
Richard<rg****@gmail.comwrites:
>"Bill Cunningham" <no****@nspam.invalidwrites:
>>"Ian Collins" <ia******@hotmail.comwrote in message
news:6j************@mid.individual.net...
<snip>
>>> unsigned n = 1;
while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;

[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?
<snip>
>To confuse you I assume. Its not a very C way of doing it IMO. Not
tested for ISO C correctness or whether it even works but this is much
clearer if you (and you must as a c programmer) understand for
loops. You should get the idea.

for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);

Great disclaimer! "This is better (but it may be wrong)". Yes it is
(wrong). Here is the flaw with using a debugger. It is easier to
think about this code than to paste this into a main function and
debug it.
I noticed it when hitting return as the thread will show you. Whoops
indeed. And I never used a debugger so where that came from I'm not
sure. My reference to a debugger before was for Bill to see where he seg
faulted with zero arguments.
Sep 20 '08 #32
Bill Cunningham <no****@nspam.invalidwrites:
Richard wrote:
>And while I am at it, please stop spamming the application development
groups telling them you are trying to write a printer driver when you
could not even tell them the type of printer you are using.

[Testing out THunderbird]

Go back a read the thread for what it says. We were conversing about
my printer make and model. I don't need to defend what's posted.
I know what was said in the thread. You told the guys you wanted to
write a printer driver for it.
Sep 20 '08 #33
Ian Collins <ia******@hotmail.comwrites:
Bill Cunningham wrote:
>"Barry Schwarz" <sc******@dqel.comwrote in message
news:7f********************************@4ax.com.. .
>>On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
>>> if ((fp = fopen(argv[4], "a")) == NULL) {
At the same time, it should be a file name.

That doesn't strike you as slightly odd?
A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you.

Like common sense?
Amazing stuff. Really. If he's a troll (and I really think he is) then
he's damn good.
Sep 20 '08 #34
Richard wrote:
Ian Collins <ia******@hotmail.comwrites:
>Bill Cunningham wrote:
>>"Barry Schwarz" <sc******@dqel.comwrote in message
news:7f********************************@4ax.com. ..
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
if ((fp = fopen(argv[4], "a")) == NULL) {
At the same time, it should be a file name.

That doesn't strike you as slightly odd?

A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you.
Like common sense?

Amazing stuff. Really. If he's a troll (and I really think he is) then
he's damn good.
Hell, I'm working on a sunny Saturday afternoon so a bit of light
entertainment is called for!

--
Ian Collins.
Sep 20 '08 #35
In article <gb**********@registered.motzarella.org>,
Richard<rg****@gmail.comwrote:
Ian Collins <ia******@hotmail.comwrites:
Bill Cunningham wrote:
"Barry Schwarz" <sc******@dqel.comwrote in message
news:7f********************************@4ax.com...
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:

if ((fp = fopen(argv[4], "a")) == NULL) {
At the same time, it should be a file name.

That doesn't strike you as slightly odd?

A filename input is an argument. I thought you treated it as such.
There are
many things that kandr2 doesn't teach you.
Like common sense?

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.
Sep 20 '08 #36
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 20 '08 #37
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 20 '08 #38
CBFalconer wrote:
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.
Did you bother to read Richard's reply?

--
Ian Collins.
Sep 20 '08 #39
On 2008-09-20, 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.
Oops! The first line should be
while(argv)
or
while(argv != NULL)

...without the dereference.

--
Andrew Poelstra ap*******@wpsoftware.net
That was a joke. Jokes in mathematics, are sometimes not funny.
-Veselin Jungic
Sep 20 '08 #40
Bill Cunningham said:
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;
I have read page 49 from top to bottom and back again, and can't even find
34 on that page, let alone x~34. Perhaps you could tell me on which line
the ~ operator is shown as x~34?

--
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 #41
Andrew Poelstra said:
On 2008-09-20, 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.

Oops! The first line should be
while(argv)
or
while(argv != NULL)

..without the dereference.
No, you were right the first time.

--
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 #42
On 2008-09-20, Richard Heathfield <rj*@see.sig.invalidwrote:
Andrew Poelstra said:
>On 2008-09-20, 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.

Oops! The first line should be
while(argv)
or
while(argv != NULL)

..without the dereference.

No, you were right the first time.
Oh, I see. I guess I need more sleep before working with
pointers to pointers any more. =P

--
Andrew Poelstra ap*******@wpsoftware.net
That was a joke. Jokes in mathematics, are sometimes not funny.
-Veselin Jungic
Sep 20 '08 #43
Andrew Poelstra <ap*******@supernova.homewrites:
On 2008-09-20, 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.

Oops! The first line should be
while(argv)
or
while(argv != NULL)

..without the dereference.
No. With the dereference. Not a typo so a fundamental misunderstanding
of the basic use of pointers there. I can only assume beer has been
consumed :-;

Sep 20 '08 #44
Ian Collins <ia******@hotmail.comwrites:
CBFalconer wrote:
>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.
Did you bother to read Richard's reply?
Interestingly enough I must admit to never having seen or heard of that
before.

And I guess the resulting C code would be something like:

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

or

for(char *p=*argv++; p; process(p));

Since you have to read the contents of the address pointed to by argv
anyway the compiler is clever enough in most cases to mean that using a
decrement and branch like argc-- is not any faster except for the last
null case.
Sep 20 '08 #45
CBFalconer <cb********@yahoo.comwrites:
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 think it is very unlikely that K&R made this mistake either with or
without the spaces. I don't have K&R2 but since there is no published
correction we can safely assume that Bill Cunningham is mistaken.

In my K&R (1 not 2) the example is:

~ finds use in expressions such as

x & ~077

--
Ben.
Sep 20 '08 #46
On Sat, 20 Sep 2008 00:47:34 -0400, CBFalconer <cb********@yahoo.com>
wrote:
>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?

--
Remove del for email
Sep 20 '08 #47
On Sat, 20 Sep 2008 00:50:59 -0400, CBFalconer <cb********@yahoo.com>
wrote:
>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.
Since there no character sequence "34" on page 49 (or 48 or 50) of K&R
2 nor is there any character sequence "x~" (with or without
intervening spaces), Bill's question makes even less sense than usual.
I expect he is just guessing at the contents of the book the same way
he guesses at the meaning of C constructs.

--
Remove del for email
Sep 20 '08 #48
On Fri, 19 Sep 2008 21:43:10 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
>
"Barry Schwarz" <sc******@dqel.comwrote in message
news:7f********************************@4ax.com.. .
>On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
>> if ((fp = fopen(argv[4], "a")) == NULL) {

At the same time, it should be a file name.

That doesn't strike you as slightly odd?
A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you. Things like this for example.
That why clc is needed.
Since you deliberately omitted the first half of the question, your
response is completely irrelevant.

However, it does offer additional proof that you are not the slightest
bit interested in learning from the advice given here.

--
Remove del for email
Sep 20 '08 #49
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.


Brian
Sep 20 '08 #50

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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.