First C program

P

pete

hokieghal99 said:
Something I just discovered.
I can declare a variable and initialize it
at the same time... this is a big time/line saver.
Is this inappropiate?

Yes.
int octet0 = 192;
int octet1 = 168;
int octet2 = 1;
int octet3 = 1;

You can have all kinds of fun with a program like that.
You can make it five octet ready:

/* BEGIN octet.c */

#include <stdio.h>

#define FN ips_c.txt
#define MODE w
#define INITIAL_VALUES {192, 168, 1, 1}
#define MAX 255
#define OCTETES (sizeof octet / sizeof*octet)
#define LAST (OCTETES - 1)
#define str(s) # s
#define xstr(s) str(s)

int main(void)
{
int octet[] = INITIAL_VALUES;
size_t byte;
FILE *fp;

fp = fopen(xstr(FN), xstr(MODE));
if (!fp) {
fp = stdout;
}
while (MAX >= octet[LAST]) {
for (byte = 0; byte != LAST; ++byte) {
fprintf(fp, "%d.", octet[byte]);
}
fprintf(fp, "%d\n", octet[LAST]);
++octet[LAST];
}
if (fp != stdout) {
fclose(fp);
puts("\nThe file '"xstr(FN)
"' was generated successfully...\n");
}
return 0;
}

/* END octet.c */
 
M

Mac

I always forget something. :) Thanks.

-Mike


You caught an awful lot of stuff. And, if the OP checks to see that his
fwrite() calls are OK, like you said, there isn't any real reason for the
fclose to fail.

Mac
--
 
K

Kevin Easton

Mac said:
You caught an awful lot of stuff. And, if the OP checks to see that his
fwrite() calls are OK, like you said, there isn't any real reason for the
fclose to fail.

It's possible for write errors to not be detected until fclose() time.

- Kevin.
 
M

Micah Cowan

Mac said:
You caught an awful lot of stuff. And, if the OP checks to see that his
fwrite() calls are OK, like you said, there isn't any real reason for the
fclose to fail.

Just because fwrite() succeeds doesn't mean anything's *actually*
been written. Typically, fclose() will cause any remaining data to be
written to be flushed out to the destination. Any problems with
this would cause fclose() to fail, and it's probably something
you should handle.

-Micah
 
M

Mike Wahler

Micah Cowan said:
Just because fwrite() succeeds doesn't mean anything's *actually*
been written. Typically, fclose() will cause any remaining data to be
written to be flushed out to the destination. Any problems with
this would cause fclose() to fail, and it's probably something
you should handle.

And to OP or anyone else listening, more generally,
if any function's documentation specifies any possible
behavior (e.g. returning a particular value) which is
the result of an error or failure, one should always
check for that failure indicator. IMO one should eschew
"it simply cannot happen" from one's mind.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top