C
CR
having a problem with the final strcpy of filename from *argv[] into a
structure containing an array with 20 elements called fname
It seems that strcpy & strncpy aren't stopping after null is found but
appending other trash overwriting the array possibly?
Any help would be appreciated!
(if you need all the code let me know)
int main(int argc, char *argv[])
{
int *arg_chk[2];
int i; int j=0;
FILE *filechecker;
struct curFile inFile[3];
int word=0;
char arg_test[20];
int w_flag=0;
char pause;
/*for(i=0; i<argc; i++){
printf("%s\n", argv); */
for(i=0; i<argc; i++){
//if(argv[0] == '-')
printf("argv[%d]=%s\n", i, argv);
scanf("%c", &pause);
}
if(argc < 3)
printf("Enter the correct parameters: grep [option] pattern
file(s)\n");
if(argc >= 3)
for(i=0; i<argc; i++)
{// i is index for which file is processed
strcpy(arg_test, argv);
if(strstr(arg_test, ".dat") && w_flag==0)
{word=i-1; w_flag=1;}
if(word && j<=MAXFILES-1)
{
printf("FILE=%d\n", j);
printf("argv[%d]=%s\n", i, argv);
if((filechecker=fopen(argv, "r"))==NULL)
{
printf("NO SUCH FILE! %s\n", argv);
exit(0);
}else{
strcpy(inFile[j].fname, argv);
printf("inFile[%d].fname = %s\n",j,
inFile[j].fname);<---(DEBUG LINE)------PROBLEM HERE WHEN j=2
check_line(filechecker, argv[word],
&inFile[j], 3); <-----------------PROBLEM HERE WHEN j=2
fclose(filechecker);
j++;
}
}// end if word
/** PUT IN IF STATEMENTS FOR EXTRA CREDIT ARGV **/
}// end for
scanf("%c", &pause);
return(0);
}// END OF MAIN FUNCTION
structure containing an array with 20 elements called fname
It seems that strcpy & strncpy aren't stopping after null is found but
appending other trash overwriting the array possibly?
Any help would be appreciated!
(if you need all the code let me know)
int main(int argc, char *argv[])
{
int *arg_chk[2];
int i; int j=0;
FILE *filechecker;
struct curFile inFile[3];
int word=0;
char arg_test[20];
int w_flag=0;
char pause;
/*for(i=0; i<argc; i++){
printf("%s\n", argv); */
for(i=0; i<argc; i++){
//if(argv[0] == '-')
printf("argv[%d]=%s\n", i, argv);
scanf("%c", &pause);
}
if(argc < 3)
printf("Enter the correct parameters: grep [option] pattern
file(s)\n");
if(argc >= 3)
for(i=0; i<argc; i++)
{// i is index for which file is processed
strcpy(arg_test, argv);
if(strstr(arg_test, ".dat") && w_flag==0)
{word=i-1; w_flag=1;}
if(word && j<=MAXFILES-1)
{
printf("FILE=%d\n", j);
printf("argv[%d]=%s\n", i, argv);
if((filechecker=fopen(argv, "r"))==NULL)
{
printf("NO SUCH FILE! %s\n", argv);
exit(0);
}else{
strcpy(inFile[j].fname, argv);
printf("inFile[%d].fname = %s\n",j,
inFile[j].fname);<---(DEBUG LINE)------PROBLEM HERE WHEN j=2
check_line(filechecker, argv[word],
&inFile[j], 3); <-----------------PROBLEM HERE WHEN j=2
fclose(filechecker);
j++;
}
}// end if word
/** PUT IN IF STATEMENTS FOR EXTRA CREDIT ARGV **/
}// end for
scanf("%c", &pause);
return(0);
}// END OF MAIN FUNCTION