F
Frank
I'm very pleased with my recent purchase of H&S V. Now that I'm not
eating the money spent, I seem to be able not to starve with a house
full of food.
I was glad to read that H&S has an on-line presence. Does anyone have
an opinion of it? Code examples? Errata? Other goodies?
I always have trouble with control structures when I switch syntaxes.
I'm looking for H&S solns to the following problem. I want this
program to spit back to the command line when argv[1] == 0 . I
believe there exists a solution in standard C.
F:\gfortran\dan>gcc j4.c -Wall -o j.exe
F:\gfortran\dan>j 0 ot3.txt
argv1 is 0
stderr has the following error 0
errno is 0
endp is 003D2445
the pointer char endp, points to [ ]:[0]
all ok
F:\gfortran\dan>type j4.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, nc;
if(n<0||n>999||path==0||*path==0) return -1;
nc=strlen(path); if(nc<=0||nc>999) return -1;
c =strlen(exp_suffix); if(c <=0||c >999) return -1;
nc+=c;
path_exp = malloc(nc + 1);
if(!path_exp)
{fprintf(stderr, "%s: %s\n", "exptab", "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");
fclose(fout);
return -1;
}
while( (c=fgetc(fin)) != EOF )
{if(c == '\t')
{nc=n;
while(nc--)
fputc(' ', fout);
}
else fputc(c, fout);
}
c=0;
if( fclose(fin) !=0 ) c=1;
if( fclose(fout)!=0 ) c=1;
return c==1?(fprintf(stderr, "%s(%d): %s\n",
__FILE__, __LINE__,"exptab"),-1):0;
}
/*test*/
#include <errno.h>
int main(int argc, char *argv[])
{long n;
char *endp;
int errnoStrtol;
if(argc != 3){fprintf(stderr, "%s: <num> <filename>\n", argv[0]);
return -1;
}
errno=0; endp=argv[1];
n=strtol(argv[1], &endp, 10);
errnoStrtol=errno;
// print some values
printf("argv1 is %ld\n" , n);
fprintf(stderr, "stderr has the following error %d\n ", errnoStrtol);
printf("errno is %d\n" , errnoStrtol);
printf("endp is %p\n" , (void*) endp);
// printf(" endp points to %p\n" , (void*)*endp);
// have no meaning:
// long strtol(char* s, char** whereIn_s, int base);
// strtol has need to change the value in the
// char pointer "endp" for doing that
// i have to pass to strtol the &endp (that is "char**" )
// so it(strtol()function) can change endp with the right value
if(endp!=0)
printf(" the pointer char endp, points to [%c]:[%d]\n" ,
endp[0], (int)endp[0]);
if (endp==argv[1] || // this mens that no number is get
errnoStrtol!=0 || // this means there were error of overflow
n>9999 // number n too big
){//perror("main");
printf("Errore nell'inserimento dell'input\n");
return EXIT_FAILURE;
}
return exptab( atoi(argv[1]), argv[2])==0?
(printf("all ok\n"), 0): EXIT_FAILURE;
}
// gcc j4.c -Wall -o j.exe
F:\gfortran\dan>
So, with the above input:
F:\gfortran\dan>j 0 ot3.txt
I'd like the program control to go through stderr, then ffprintf to
stderr "do you really want to erase the tabs completely (Y/N)?", then
pop me back to command line.
What I can't see here I think I can express through 2 questions:
q1) If I want the comman line back, do I send a system command with
nothing in it?
q2) How does a c program remember previous data on argv?
For the sake of topicality I'd like to keep the notation consistent
with H&S V. Thanks for your comment,
eating the money spent, I seem to be able not to starve with a house
full of food.
I was glad to read that H&S has an on-line presence. Does anyone have
an opinion of it? Code examples? Errata? Other goodies?
I always have trouble with control structures when I switch syntaxes.
I'm looking for H&S solns to the following problem. I want this
program to spit back to the command line when argv[1] == 0 . I
believe there exists a solution in standard C.
F:\gfortran\dan>gcc j4.c -Wall -o j.exe
F:\gfortran\dan>j 0 ot3.txt
argv1 is 0
stderr has the following error 0
errno is 0
endp is 003D2445
the pointer char endp, points to [ ]:[0]
all ok
F:\gfortran\dan>type j4.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, nc;
if(n<0||n>999||path==0||*path==0) return -1;
nc=strlen(path); if(nc<=0||nc>999) return -1;
c =strlen(exp_suffix); if(c <=0||c >999) return -1;
nc+=c;
path_exp = malloc(nc + 1);
if(!path_exp)
{fprintf(stderr, "%s: %s\n", "exptab", "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");
fclose(fout);
return -1;
}
while( (c=fgetc(fin)) != EOF )
{if(c == '\t')
{nc=n;
while(nc--)
fputc(' ', fout);
}
else fputc(c, fout);
}
c=0;
if( fclose(fin) !=0 ) c=1;
if( fclose(fout)!=0 ) c=1;
return c==1?(fprintf(stderr, "%s(%d): %s\n",
__FILE__, __LINE__,"exptab"),-1):0;
}
/*test*/
#include <errno.h>
int main(int argc, char *argv[])
{long n;
char *endp;
int errnoStrtol;
if(argc != 3){fprintf(stderr, "%s: <num> <filename>\n", argv[0]);
return -1;
}
errno=0; endp=argv[1];
n=strtol(argv[1], &endp, 10);
errnoStrtol=errno;
// print some values
printf("argv1 is %ld\n" , n);
fprintf(stderr, "stderr has the following error %d\n ", errnoStrtol);
printf("errno is %d\n" , errnoStrtol);
printf("endp is %p\n" , (void*) endp);
// printf(" endp points to %p\n" , (void*)*endp);
// have no meaning:
// long strtol(char* s, char** whereIn_s, int base);
// strtol has need to change the value in the
// char pointer "endp" for doing that
// i have to pass to strtol the &endp (that is "char**" )
// so it(strtol()function) can change endp with the right value
if(endp!=0)
printf(" the pointer char endp, points to [%c]:[%d]\n" ,
endp[0], (int)endp[0]);
if (endp==argv[1] || // this mens that no number is get
errnoStrtol!=0 || // this means there were error of overflow
n>9999 // number n too big
){//perror("main");
printf("Errore nell'inserimento dell'input\n");
return EXIT_FAILURE;
}
return exptab( atoi(argv[1]), argv[2])==0?
(printf("all ok\n"), 0): EXIT_FAILURE;
}
// gcc j4.c -Wall -o j.exe
F:\gfortran\dan>
So, with the above input:
F:\gfortran\dan>j 0 ot3.txt
I'd like the program control to go through stderr, then ffprintf to
stderr "do you really want to erase the tabs completely (Y/N)?", then
pop me back to command line.
What I can't see here I think I can express through 2 questions:
q1) If I want the comman line back, do I send a system command with
nothing in it?
q2) How does a c program remember previous data on argv?
For the sake of topicality I'd like to keep the notation consistent
with H&S V. Thanks for your comment,