F
Frank
Lovecreatesbeauty posted a soln to Malcolm's detabbing challenge which
I'd like to address again. It seems to behave:
F:\gfortran\dan>k t50 ot3.txt
main: strtol error
F:\gfortran\dan>k 555555555555555555555 ot3.txt
main: Result too large
F:\gfortran\dan>type k2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int exptab(int n, const char *path)
{
FILE *fin, *fout;
char *path_exp, *exp_suffix = ".exp";
int c;
const int cn = n;
path_exp = malloc(strlen(path) + strlen(exp_suffix) + 1);
if (!path_exp){
fprintf(stderr, "%s: %s\n", __func__, "malloc error");
return -1;
}
strcat(strcpy(path_exp, path), exp_suffix);
fout = fopen(path_exp, "w");
free(path_exp);
if (!fout){
fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, "fopen");
return -1;
}
fin = fopen(path, "r");
if (!fin){
fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, "fopen");
return -1;
}
while ((c=fgetc(fin)) != EOF){
n = cn;
if (c == '\t') while (n--) fputc(' ', fout);
else fputc(c, fout);
}
fclose(fin);
fclose(fout);
return 0;
}
/*test*/
#include <errno.h>
int main(int argc, char *argv[])
{
int n;
char *endp;
if (argc != 3){
fprintf(stderr, "%s: <num> <filename>\n", argv[0]);
return -1;
}
errno = 0;
n = strtol(argv[1], &endp, 10);
if (n == LONG_MIN || n == LONG_MAX){
perror(__func__);
return -1;
}
if (*endp != '\0'){
fprintf(stderr, "%s: %s\n", __func__, "strtol error");
return -1;
}
exptab(atoi(argv[1]), argv[2]);
return 0;
}
// gcc k2.c -Wall -o k.exe
F:\gfortran\dan>
Wouldn't it be better if n were declared as a long or to use strtod
instead?
I can't quite figure out what's going on with endp using K&R as a
reference. Under what conditions is it null?
Thanks for your comment,
I'd like to address again. It seems to behave:
F:\gfortran\dan>k t50 ot3.txt
main: strtol error
F:\gfortran\dan>k 555555555555555555555 ot3.txt
main: Result too large
F:\gfortran\dan>type k2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int exptab(int n, const char *path)
{
FILE *fin, *fout;
char *path_exp, *exp_suffix = ".exp";
int c;
const int cn = n;
path_exp = malloc(strlen(path) + strlen(exp_suffix) + 1);
if (!path_exp){
fprintf(stderr, "%s: %s\n", __func__, "malloc error");
return -1;
}
strcat(strcpy(path_exp, path), exp_suffix);
fout = fopen(path_exp, "w");
free(path_exp);
if (!fout){
fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, "fopen");
return -1;
}
fin = fopen(path, "r");
if (!fin){
fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, "fopen");
return -1;
}
while ((c=fgetc(fin)) != EOF){
n = cn;
if (c == '\t') while (n--) fputc(' ', fout);
else fputc(c, fout);
}
fclose(fin);
fclose(fout);
return 0;
}
/*test*/
#include <errno.h>
int main(int argc, char *argv[])
{
int n;
char *endp;
if (argc != 3){
fprintf(stderr, "%s: <num> <filename>\n", argv[0]);
return -1;
}
errno = 0;
n = strtol(argv[1], &endp, 10);
if (n == LONG_MIN || n == LONG_MAX){
perror(__func__);
return -1;
}
if (*endp != '\0'){
fprintf(stderr, "%s: %s\n", __func__, "strtol error");
return -1;
}
exptab(atoi(argv[1]), argv[2]);
return 0;
}
// gcc k2.c -Wall -o k.exe
F:\gfortran\dan>
Wouldn't it be better if n were declared as a long or to use strtod
instead?
I can't quite figure out what's going on with endp using K&R as a
reference. Under what conditions is it null?
Thanks for your comment,