Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 23rd, 2006, 3:44 AM   #1
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
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.
squirrel is offline   Reply With Quote
Old Jun 23rd, 2006, 4:06 AM   #2
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 342
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
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
mrynit is offline   Reply With Quote
Old Jun 23rd, 2006, 4:15 AM   #3
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
Yes, It is the snip of the total code.
Iam new to programing and not knowing how to modify.
squirrel is offline   Reply With Quote
Old Jun 23rd, 2006, 4:46 AM   #4
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
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);
}
squirrel is offline   Reply With Quote
Old Jun 23rd, 2006, 7:36 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
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
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 7:37 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
sscanf(argv[1],"%d",&n2);
HTH.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Jun 23rd, 2006, 7:40 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 8:01 AM   #8
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Old Jun 23rd, 2006, 8:05 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 9:26 AM   #10
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
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!!
squirrel is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:45 AM.

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