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]
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?
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);
}
thanx
squirrel