944,052 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11995
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 10th, 2007
0

command line arguments help

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ck1212 is offline Offline
4 posts
since Apr 2007
Apr 10th, 2007
0

Re: command line arguments help

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Apr 10th, 2007
0

Re: command line arguments help

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ck1212 is offline Offline
4 posts
since Apr 2007
Apr 10th, 2007
0

Re: command line arguments help

>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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 10th, 2007
0

Re: command line arguments help

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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ck1212 is offline Offline
4 posts
since Apr 2007
Apr 10th, 2007
0

Re: command line arguments help

>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).
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 10th, 2007
0

Re: command line arguments help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ck1212 is offline Offline
4 posts
since Apr 2007
Apr 10th, 2007
0

Re: command line arguments help

>>//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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Apr 11th, 2007
0

Re: command line arguments help

Please read this so we can read your code easier.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Apr 11th, 2007
0

Re: command line arguments help

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Function declaration error
Next Thread in C Forum Timeline: Handling I/O redirections in my shell.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC