Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   compile program with command line arguments (http://www.programmingforums.org/showthread.php?t=10499)

squirrel Jun 23rd, 2006 3:44 AM

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

mrynit Jun 23rd, 2006 4:06 AM

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.

squirrel Jun 23rd, 2006 4:15 AM

Yes, It is the snip of the total code.
Iam new to programing and not knowing how to modify.

squirrel Jun 23rd, 2006 4:46 AM

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);
}


DaWei Jun 23rd, 2006 7:36 AM

:

main(int argc,char *argv[])
Argc contains the number of items on the command line, including the command. Argv is an array of C-strings, one for each item. Argv [0] is the command, argv [1] is the first argument, argv [2] is the second argument, etc.

InfoGeek Jun 23rd, 2006 7:37 AM

:

sscanf(argv[1],"%d",&n2);
HTH.

DaWei Jun 23rd, 2006 7:40 AM

Now, Info, I can't believe you recommend that schlocky code. What if the user doesn't put a number there?

InfoGeek Jun 23rd, 2006 8:01 AM

Sorry for that, Dawei.
I'd be more careful while giving advice next time.
:

if(sscanf(argv[1],"%d",&n2)!=1)
  Uhoh(TroubleInRiverCity);


DaWei Jun 23rd, 2006 8:05 AM

LOL, attaboy ;) .

squirrel Jun 23rd, 2006 9:26 AM

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!!


All times are GMT -5. The time now is 8:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC