cmay
June 20th, 2008, 07:34 PM
hi.
this is my first c program.i have only programmed in a month.
its my version of the cat command. i have however this question
if its bad practice to declare int argc ,char argv in a function ?.
the program compiles and works fine.
i dont have any class to follow or any other information than the
tutorials i find on the net. and a single book i got on cc++.
i never found any specific information on this.
i declare this in the out function and the main as usual .
i could not make it compile otherwise.
is this a bad thing to do ?
my book mention a parameter getenv[] instead of argv[]
its kind of old.
thanks for any input.
#include <stdio.h>
#include <stdlib.h>
char PROGNAME[]="dog";
FILE *infile;
FILE *outputfile;
int my_flag;
/***************: all globals:***********/
int out();
void help();
void version();
void filecopy(FILE *inputfile,FILE *outputfile);
/********:the prototypes:****************/
int main(int argc,char *argv[]){
int c;
char *char_ptr;
while( argc > 1 && *(char_ptr=argv[1])=='-'){
argc--;
argv++;
switch(*++char_ptr){
case '?':
printf("i dont know \nwhy do you ask me \?\n");
exit(0);
case 'v':
version();
exit(0);
break;
case 'h':
help();
exit(0);
break;
case 'c':
my_flag++;
break;
default:
fprintf(stderr,"%s: unknown flag\n",PROGNAME);
exit(1);
}
}
/*
* if there is only argument then usage : blahh blahh
*/
if(argc==1){
printf("USAGE: blahh blahh\n");
exit(1);
}
else
for(c=1;c<argc;c++)
if((infile=fopen(argv[c],"r"))==NULL)
fprintf(stderr,"%s: cant open %s \n",PROGNAME,argv[c]);
else
{
out(argc,argv);
fclose(infile);
}
exit(0);
}
/*********:end of main*****************/
void help(){
printf(" flag -v version \n");
printf(" flag -c works like cat command\n");
printf("so does no flag \n");
printf("\n");
}
int out(int argc, char *argv[])
{
FILE *filepointer;
void filecopy(FILE *,FILE *);
/* use this to give error message*/
char *prog =argv[0];
/* if there is no arguements then there is no need to argue*/
if(argc == 1)
filecopy(stdin,stdout);
else
while(--argc > 0)
if((filepointer=fopen(*++argv,"r"))==NULL)
{
fprintf(stderr,"%s: cant open %s\n",prog,*argv);
exit(1);
} else {
filecopy(filepointer,stdout);
fclose(filepointer);
}
if(ferror(stdout))
{
fprintf(stderr,"%s:error in writing to stdout \n",prog);
exit(2);
}
exit(0);
}
void filecopy(FILE *inputfil,FILE *outputfil)
{
int c;
while((c=getc(inputfil))!=EOF)
putc(c,outputfil);
}
void version(){
printf("***************************************\n");
printf("* program :dog *\n");
printf("* version :this is my first c program *\n");
printf("* author :cmay *\n");
printf("*license :lgpl *\n");
printf("***************************************\n");
}
this is my first c program.i have only programmed in a month.
its my version of the cat command. i have however this question
if its bad practice to declare int argc ,char argv in a function ?.
the program compiles and works fine.
i dont have any class to follow or any other information than the
tutorials i find on the net. and a single book i got on cc++.
i never found any specific information on this.
i declare this in the out function and the main as usual .
i could not make it compile otherwise.
is this a bad thing to do ?
my book mention a parameter getenv[] instead of argv[]
its kind of old.
thanks for any input.
#include <stdio.h>
#include <stdlib.h>
char PROGNAME[]="dog";
FILE *infile;
FILE *outputfile;
int my_flag;
/***************: all globals:***********/
int out();
void help();
void version();
void filecopy(FILE *inputfile,FILE *outputfile);
/********:the prototypes:****************/
int main(int argc,char *argv[]){
int c;
char *char_ptr;
while( argc > 1 && *(char_ptr=argv[1])=='-'){
argc--;
argv++;
switch(*++char_ptr){
case '?':
printf("i dont know \nwhy do you ask me \?\n");
exit(0);
case 'v':
version();
exit(0);
break;
case 'h':
help();
exit(0);
break;
case 'c':
my_flag++;
break;
default:
fprintf(stderr,"%s: unknown flag\n",PROGNAME);
exit(1);
}
}
/*
* if there is only argument then usage : blahh blahh
*/
if(argc==1){
printf("USAGE: blahh blahh\n");
exit(1);
}
else
for(c=1;c<argc;c++)
if((infile=fopen(argv[c],"r"))==NULL)
fprintf(stderr,"%s: cant open %s \n",PROGNAME,argv[c]);
else
{
out(argc,argv);
fclose(infile);
}
exit(0);
}
/*********:end of main*****************/
void help(){
printf(" flag -v version \n");
printf(" flag -c works like cat command\n");
printf("so does no flag \n");
printf("\n");
}
int out(int argc, char *argv[])
{
FILE *filepointer;
void filecopy(FILE *,FILE *);
/* use this to give error message*/
char *prog =argv[0];
/* if there is no arguements then there is no need to argue*/
if(argc == 1)
filecopy(stdin,stdout);
else
while(--argc > 0)
if((filepointer=fopen(*++argv,"r"))==NULL)
{
fprintf(stderr,"%s: cant open %s\n",prog,*argv);
exit(1);
} else {
filecopy(filepointer,stdout);
fclose(filepointer);
}
if(ferror(stdout))
{
fprintf(stderr,"%s:error in writing to stdout \n",prog);
exit(2);
}
exit(0);
}
void filecopy(FILE *inputfil,FILE *outputfil)
{
int c;
while((c=getc(inputfil))!=EOF)
putc(c,outputfil);
}
void version(){
printf("***************************************\n");
printf("* program :dog *\n");
printf("* version :this is my first c program *\n");
printf("* author :cmay *\n");
printf("*license :lgpl *\n");
printf("***************************************\n");
}