473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

check out this code! any ideas for me fellas?

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[i]); */

for(i=0; i<argc; i++){
//if(argv[i][0] == '-')
printf("argv[%d]=%s\n", i, argv[i]);
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[i]);
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[i]);

if((filechecker=fopen(argv[i], "r"))==NULL)
{
printf("NO SUCH FILE! %s\n", argv[i]);
exit(0);
}else{
strcpy(inFile[j].fname, argv[i]);
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[i] **/
}// end for
scanf("%c", &pause);
return(0);
}// END OF MAIN FUNCTION
Nov 13 '05 #1
2 1606

"CR" <fa*******@adelphia.net> schrieb im Newsbeitrag
news:sC********************@news1.news.adelphia.ne t...
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[i]); */

for(i=0; i<argc; i++){
//if(argv[i][0] == '-')
printf("argv[%d]=%s\n", i, argv[i]);
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[i]);
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[i]);

if((filechecker=fopen(argv[i], "r"))==NULL)
{
printf("NO SUCH FILE! %s\n", argv[i]);
exit(0);
}else{
strcpy(inFile[j].fname, argv[i]);
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[i] **/
}// 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[i]);
/*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[i]);
/*In your version:
If strlen(argv[i]) > 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[i][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[i]);

if((filechecker = fopen(argv[i], "r"))==NULL)
{
printf("NO SUCH FILE! %s\n", argv[i]);
/*Why do you give up here if just one of the requested files
is missing?*/
exit(0);
}else
{
strcpy(inFile[j].fname, argv[i]);
/*In your version this will screw up everything if:
MAXFILES < 3 (you access a struct outside of your array)
or/and
strlen(argv[i]) > 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
Nov 13 '05 #2
CR

"Robert Stankowic" <pc******@netway.at> wrote in message
news:3f***********************@newsreader01.highwa y.telekom.at...

"CR" <fa*******@adelphia.net> schrieb im Newsbeitrag
news:sC********************@news1.news.adelphia.ne t...
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!)
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jay | last post by:
Hi ! I am a beginner in PHP. Does anybody know how to check if an URL is alive or dead in PHP (not exist or server down...or whatever ) ? Any help would be appreciated ! Thanks ! Jay
2
by: Askari | last post by:
Hi, How do for do a "select()" on a CheckButton in a menu (make with add_checkbutton(....) )? I can modify title, state, etc but not the "check state". :-( Askari
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
7
by: Tony Johnson | last post by:
Can you make a check box very big? It seems like when you drag it bigger the little check is still the same size. Thank you, *** Sent via Developersdex http://www.developersdex.com ***...
1
by: Helixpoint | last post by:
I upload a file with the following code. Is there a way to check to see if the file is currently there before I upload? Dim s1 As String Dim s2 As String Dim pos As Integer s1 =...
3
by: Al | last post by:
Hello, Sorry for the silly question: How do I insert Check Mark into RichTextBox? I can’t find the ascii code for it. Thanks Alex
2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
22
tbarto
by: tbarto | last post by:
Hi everybody For some time I have been working on an UI testing application. The application under test was developed in C#, and my application has been developed in C# as well. I do not have a...
55
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of...
1
by: ghjk | last post by:
my php page has 7 check boxes. I stored checked values to database and retrive as binary values. This is the result array Array ( => 0 => 1 => 0 => 1 => 0 => 0 => 1 ) 1 means checked....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.