command line arguments help

Reply

Join Date: Apr 2007
Posts: 4
Reputation: ck1212 is an unknown quantity at this point 
Solved Threads: 0
ck1212 ck1212 is offline Offline
Newbie Poster

command line arguments help

 
0
  #1
Apr 10th, 2007
i need ur help to complete this assignment

Write a C program which, reads from standard input, replaces all the occurrences of the characters supplied by the 1st command line argument, with the corresponding characters supplied by the 2nd command line argument, and writes the output to a file
supplied by the 3rd command line argument, then print out the file.

any help will be much appreciated
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: command line arguments help

 
0
  #2
Apr 10th, 2007
post the code for what you know how to code and ask questions. We'll be glad to help, but we don't write it for you.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: ck1212 is an unknown quantity at this point 
Solved Threads: 0
ck1212 ck1212 is offline Offline
Newbie Poster

Re: command line arguments help

 
0
  #3
Apr 10th, 2007
i understand this simple program, but unsure of how to modify it to meet my objective.
  1. include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5. int x;
  6.  
  7. printf("%d\n",argc);
  8. for (x=0; x<argc; x++)
  9. printf("%s\n",argv[x]);
  10. return 0;
  11. }
also when I compile the code to an executable file named aaa and type aaa xxx yyy zzz. The code will print the command line parameters xxx, yyy and zzz, one per line. are these the command line arguments that i need to replace?? so xxx would be the 1st command line argument and yyy would be the second command line argument.
is this correct or have i got it completely wrong
Last edited by WaltP; Apr 10th, 2007 at 2:03 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: command line arguments help

 
0
  #4
Apr 10th, 2007
>The code will print the command line parameters xxx, yyy and zzz, one per line.
Actually, the first thing that program will print out is the name of the executable. The first argument that's passed to the program is actually the second element in 'argv'.

>are these the command line arguments that i need to replace??
Yes.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: ck1212 is an unknown quantity at this point 
Solved Threads: 0
ck1212 ck1212 is offline Offline
Newbie Poster

Re: command line arguments help

 
0
  #5
Apr 10th, 2007
i had an idea of puting the command line arguments onto stack. so when i pop them they will be reversed.
would this work??
how do i write code for this can any1 help??
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: command line arguments help

 
0
  #6
Apr 10th, 2007
>i had an idea of puting the command line arguments onto stack.
What would be the purpose of that? If you want them reversed, make a reverse loop (counting down to 0).
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: ck1212 is an unknown quantity at this point 
Solved Threads: 0
ck1212 ck1212 is offline Offline
Newbie Poster

Re: command line arguments help

 
0
  #7
Apr 10th, 2007
hi again. i have this code so far. but it doesnt seem to be working as i'd hoped. can anyone give me a few tips. thanks
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. void oops(char *s1, char *s2);
  7. main(int argc, char *argv[] )
  8. {
  9. int write_fd, nwrite;
  10. char buffer[100];
  11. char *location;
  12. int length=strlen(argv[1]);
  13. if(argc != 4)
  14. { //check that the arguments are given correctly.
  15. printf("Error: Too many arguments!\nFilename should have no spaces.\n\nUsage: %s string1 string2 filename\n",argv[0]);
  16. exit(1);
  17. }
  18. if(strlen(argv[1])!=strlen(argv[2]))
  19. { //check whether the initial and replacement strings are of same size.
  20. printf("Error: string 1 must be of equal length to string \n");
  21. exit(1);
  22. }
  23. printf("Input text and press enter:\n");
  24. gets(buffer);
  25. //put text from stdin into string buffer
  26. printf("Replacing all occurances of \"%s\" with \"%s\" in \"s\"\n",argv[1],argv[2],buffer);
  27. while(location=strstr(buffer,argv[1]))
  28. //replace all string1's with string2's in buffer
  29. { strncpy(location,argv[2],length);
  30. }
  31. /*write to file*/
  32. printf("Writing to file... \n");
  33. if ( (write_fd=creat( argv[3], O_WRONLY)) == -1 )
  34. oops( "Cannot create file", argv[3]);
  35. if ( write( write_fd, buffer, strlen(buffer) ) != strlen(buffer) )
  36. oops("Write error to ", argv[3]); /* close file */
  37. if (close(write_fd) == -1 )
  38. oops("Error closing files","");
  39. printf("Done.\nResult:"); printf("%s\n",buffer); //output result}
  40. void oops(char *s1, char *s2){ fprintf(stderr,"Error: %s ", s1); perror(s2); exit(1);}
Last edited by WaltP; Apr 11th, 2007 at 4:09 am. Reason: Added CODE tags again -- you formatting is worthless, too.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: command line arguments help

 
0
  #8
Apr 10th, 2007
>>//check whether the initial and replacement strings are of same size.

That check does not meed the program's spefications -- there was nothing in the assignment you posted that said the replacement string must be the same length as the search string.

>>creat( argv[3], O_WRONLY

why are you using those deprecated functions? Use standard C FILE and associated functions.

>>gets(buffer);
never ever and at no time use gets() because it can cause your program to crash big-time. Use fgets() instead to limit the keyboard input to the size of the input buffer.

Please please format your code so that you and everyone else can read it. I was going to add the code tags for you but it was just too badly formatted for code tags to do any good.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: command line arguments help

 
0
  #9
Apr 11th, 2007
Please read this so we can read your code easier.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: command line arguments help

 
0
  #10
Apr 11th, 2007
aha but is this the right logic Ancient Dragon ?!
coz looking at his code he seems that he used a totally different logic !

is this the right test for the program ?!

c.exe abc def file.txt
argv[1]=abc
argv[2]=def
argv[3]=file.txt

input: abc
buffer = def
input :cajef
buffer= fdjef?!

because looking at the given "occurrences of the characters" could be the position of the characters or the same characters...
and that what make me lost.
Last edited by rowly; Apr 11th, 2007 at 10:08 am.
DarkCoder+
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC