F
Frank
You are just posting code at random. Why did you change strtol to
strtod when you are expecting an integer value?
Thanks, again, for your meticulous reading of source that I thought
was closer to done than was the case. I think this version hits the
criticisms you made:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int n;
FILE *fin, *fout;
char *path_exp, *exp_suffix = ".exp";
const char *path;
int c, nc;
char d;
if (argc != 3)
{
fprintf(stderr, "usage: \n");
fprintf(stderr, "%s <num> <filename>\n", argv[0]);
return EXIT_FAILURE;
}
path=argv[2];
errno = 0;
n = atoi(argv[1]);
if (n < 0 || n > 999 )
{
fprintf(stderr, "%s: give me a natural number"
" less than 1000\n", argv[0]);
return EXIT_FAILURE;
}
// if detab
if (n == 0)
{
fprintf(stderr, "this will detab your file\n");
fprintf(stderr, "If that's what you want, enter zero again\n");
fprintf(stderr, "To abort, enter anything else\n");
d = getchar();
if (d == '0')
fprintf(stderr, "detabbing %s \n", argv[2]);
else
{
fprintf(stderr, "Aborting");
return EXIT_FAILURE;
}
}
// Process path and open file
nc = strlen (path);
c = strlen (exp_suffix);
path_exp = malloc(c + nc + 1);
if (!path_exp)
{
fprintf(stderr, "malloc error: %s\n", __func__);
return EXIT_FAILURE;
}
strcat(strcpy(path_exp, path), exp_suffix);
fin = fopen(path, "r");
if (!fin)
{
fprintf(stderr, "%s %s\n", "fopen failed to open", path );
return EXIT_FAILURE;
}
// main control
// print some values and exit
printf(" argv1 is %d\n" , n);
printf(" argv2 is %s\n" , path);
fout = fopen(path_exp, "w");
if (!fout)
{
fprintf(stderr, "%s \n", "fout failed");
return EXIT_FAILURE;
}
fclose(fin);
fclose(fout);
free(path_exp);
return EXIT_SUCCESS;
}
// gcc hh1.c -Wall -o indent.exe
I think the fclose situation looks common-sensical now. I guess I
don't know the circumstances where fout is going to fail, given that
fin succeeded. On my platform, if this little program returns
EXIT_FAILURE, then files it opened up are going to live for about a
millisecond.
You are just posting code at random. Why did you change strtol to
strtod when you are expecting an integer value?
Anything but. I had strol, and on pg 412 H&S, they give this entire
family. Since there had to be one of them for just plain int, I
deduced that it had to be strtod, following the format specifier for
printing an int.
K&R §B5 has
int atoi(const char * s)
is equivalent to
(int) strtol(s, (char **)NULL, 10)
Only yesterday did I get rid of endp and put NULL in its place, which,
apparently, should have been casted. This is not random. When I try
something new, it has about a 70-30 chance of being right. This aspect
of learning is, from my perspective, stochastic.