![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2006
Posts: 11
Rep Power: 0
![]() |
compile program with command line arguments
Hi,
I have a program which is hard coded, I need to enter a value(n2 in the below code) in the program, and the program processes the file according to the value n2. how I compile now is as follows: cc -o test testprog.c ./test [filename] if((data[0]=='8')&&(data[1]=='2'))
{
memcpy(&l1,data+2,sizeof(int));
memcpy(&l2,data+6,sizeof(int));
memcpy(&n1,data+13,sizeof(int));
memcpy(&n2,data+17,sizeof(int));
memcpy(&n3,data+21,sizeof(int));
/* if(n2==81798)*/
/* if(n2==82060)*/
/* if(n2==82169)*/
/* if(n2==1795) */
/* if (n2 == 1854) */
/* if (n2 == 1830)*/
if (n2 == 41388)
{
printf("\t<Sin> 1stNode [%d] 1stLink [%d] 2ndNode [%d] 2ndLink [%d] 3rdNode [%d]\n",n1,l1,n2,l2,n3);
}
else
{
fwrite(data,128,1,fp_out);
}
iSin++;
}What I need to do is, modify the program in such a way that, the value n2 can be given as an argument at command line. Can anyone please help me with this? thanx Squirrel Last edited by squirrel; Jun 23rd, 2006 at 3:55 AM. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
i dont do C but can you just ask for user input and use scanf() to get it?? and the code you posted, is that just a snip of the total code? cause you need some more stuff for it to compile right.
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2006
Posts: 11
Rep Power: 0
![]() |
Yes, It is the snip of the total code.
Iam new to programing and not knowing how to modify. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2006
Posts: 11
Rep Power: 0
![]() |
Here is the full code:
#include "check_2ndkj.h"
int CS_write();
main(int argc,char *argv[])
{
int i_FValue;
FILE *fp_MYFILE;
if(argc != 2)
{
printf("\tErr -> command line\n");
printf("\t MYFileName\n");
exit(NO);
}
else
{
printf("\tCommand accepted -> check_K source %s\n",argv[1]);
}
/*----------------------------------------------------------*/
/* fopen */
/*----------------------------------------------------------*/
if(NULL==(fp_MYFILE=fopen(argv[1],"r")))
{
printf("file open Err! -> %s\n",argv[1]);
exit(NO);
}
/*----------------------------------------------------------*/
/*CS_write() */
/*----------------------------------------------------------*/
i_FValue = CS_write(fp_MYFILE);
if(i_FValue==NO)
{
printf("\tErr CS_write( )\n");
exit(NO);
}
/*----------------------------------------------------------*/
/* fclose */
/*----------------------------------------------------------*/
fclose(fp_MYFILE);
}
/*----------------------------------------------------------*/
/* CS_write( ) */
/*----------------------------------------------------------*/
CS_write(fp_MYFILE)
FILE *fp_MYFILE;
{
unsigned char data[128];
int i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum;
int i_cnt;
int n1,l1,n2,l2,n3;
int iSin,iIpo;
int i_1;
FILE *fp_out;
if(NULL==(fp_out=fopen("Uxxxxxx","w")))
{
printf("file open Err! -> Uxxxxxx\n");
exit(NO);
}
printf("\tCS_write_Sin( )\n");
iSin=0;
iIpo=0;
/*------------------*/
/* store */
/*------------------*/
fread(data,sizeof(data),1,fp_MYFILE);
memcpy(&i_KjNum,data+91,sizeof(i_KjNum));
memcpy(&i_NetworkNum,data+95,sizeof(i_NetworkNum));
memcpy(&i_PolygonNum,data+99,sizeof(i_PolygonNum));
memcpy(&i_TNum,data+103,sizeof(i_TNum));
memcpy(&i_ZNum,data+107,sizeof(i_ZNum));
memcpy(&i_SNum,data+111,sizeof(i_SNum));
memcpy(&i_KNum,data+115,sizeof(i_KNum));
i_1=i_KNum-1;
memcpy(&(data[115]),&i_1,sizeof(i_KNum));
fwrite(data,128,1,fp_out);
/*check*/
printf("%d %d %d %d %d %d %d \n", i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum);
for(i_cnt=0;i_cnt<i_KjNum+i_NetworkNum+i_PolygonNum+i_TNum+i_ZNum+i_SNum;i_cnt++)
{
memset(data,0x00,128);
fread(data,sizeof(data),1,fp_MYFILE);
fwrite(data,128,1,fp_out);
}
for(i_cnt=0;i_cnt<i_KNum;i_cnt++)
{
fread(data,sizeof(data),1,fp_MYFILE);
if((data[0]=='8')&&(data[1]=='2'))
{
memcpy(&l1,data+2,sizeof(int));
memcpy(&l2,data+6,sizeof(int));
memcpy(&n1,data+13,sizeof(int));
memcpy(&n2,data+17,sizeof(int));
memcpy(&n3,data+21,sizeof(int));
/* if (n2 == 1854) */
/* if (n2 == 1830)*/
if (n2 == 41388
{
printf("\t<Sin> 1stNode [%d] 1stLink [%d] 2ndNode [%d] 2ndLink [%d] 3rdNode [%d]\n",n1,l1,n2,l2,n3);
}
else
{
fwrite(data,128,1,fp_out);
}
iSin++;
}
if((data[0]=='8')&&(data[1]=='1'))
{
fwrite(data,128,1,fp_out);
iIpo++;
}
}
printf("\t iIpo [%d] iSin [%d]\n",iIpo,iSin);
fclose(fp_out);
return(OK);
} |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
main(int argc,char *argv[])
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
sscanf(argv[1],"%d",&n2);
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Now, Info, I can't believe you recommend that schlocky code. What if the user doesn't put a number there?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Sorry for that, Dawei.
I'd be more careful while giving advice next time. if(sscanf(argv[1],"%d",&n2)!=1) Uhoh(TroubleInRiverCity);
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
LOL, attaboy
.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jun 2006
Posts: 11
Rep Power: 0
![]() |
Wat do I need to add or change at the IF loop of n2 ?? Can u please explain me a bit more clearly?? Sorry for the trouble!!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|