check out this code! any ideas for me fellas?

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
 
R

Robert Stankowic

CR said:
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


Just reformatting and some comments:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*You are your own enemy :)
First of all, your formatting makes the code very hard to read
I have reformatted it a little bit
Second - you are defining MAXFILES somewhere (I hope),
but then you define an array of structs with the magic number 3 as it's
size.
If MAXFILES > 3 and the program is called with more than 3 .dat filenames --
boom
Better use MAXFILES as the size of your array
Third - your char arrays may overflow. There can be filenames with ways more
than 19 characters.
Better use the constant FILENAME_MAX defined in stdio.h.
and check the length of the filenames from argv anyway.
Furthermore please don't use C99 style comments. not all of us have a C99
compiler,
and even if we have such a shiny beast, the code will not compile if the
comment wraps*/

#define MAXFILES 3
int main(int argc, char *argv[])
{
int i;
int j=0;
FILE *filechecker;
struct curFile
{
char fname[FILENAME_MAX];
}inFile[MAXFILES];
int word=0;
char arg_test[FILENAME_MAX];
int w_flag=0;
char pause;

for(i=0; i < argc; i++)
{
printf("argv[%d]=%s\n", i, argv);
/*This is not very user friendly - the program displays argv[0] and
then stops,
leaving the user scratching his/her head..
scanf("%c", &pause);
better would be:*/
}
puts("Press enter to continue");
scanf("%c", &pause);

if(argc < 3)
{
printf("Enter the correct parameters: grep [option] pattern
file(s)\n");
}
else
{
for(i=0; i < argc; i++)
{
strcpy(arg_test, argv);
/*In your version:
If strlen(argv) > 19 strcpy will write outside your char
array -- boom*/
if(strstr(arg_test, ".dat") && w_flag == 0)
/*Hmm - you request a pattern as commandline parameter, but ignore
it here.
I assume,your code is just a cut-down version for testing -
the statement if(argv[0] == '-') suggests that*/
{
word = i - 1;
w_flag = 1;
}
/*"<= MAXFILES - 1" is a pretty weird way of writing "< MAXFILES"*/
if(word && j < MAXFILES)
{
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);
/*Why do you give up here if just one of the requested files
is missing?*/
exit(0);
}else
{
strcpy(inFile[j].fname, argv);
/*In your version this will screw up everything if:
MAXFILES < 3 (you access a struct outside of your array)
or/and
strlen(argv) > 19 (strcpy will write outside your char
arrays)
and then all bets are off*/
printf("inFile[%d].fname = %s\n", j, inFile[j].fname);
fclose(filechecker);
j++;
}
}
}
}
scanf("%c", &pause);
return EXIT_SUCCESS;
}


Robert
 
C

CR

Robert Stankowic said:
CR said:
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)


Thank you so very much for your comments Robert, much of the code was
editted for debugging purposes but I the style-pointers are much needed and
appreciated. Thanks for your time and energy reviewing the code (i'm in the
learning stages it really helps to know that some people are willing to
help!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top