| | |
command line arguments help
![]() |
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
i understand this simple program, but unsure of how to modify it to meet my objective.
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
c Syntax (Toggle Plain Text)
include <stdio.h> int main(int argc, char *argv[]) { int x; printf("%d\n",argc); for (x=0; x<argc; x++) printf("%s\n",argv[x]); return 0; }
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...
>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.
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."
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
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
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> void oops(char *s1, char *s2); main(int argc, char *argv[] ) { int write_fd, nwrite; char buffer[100]; char *location; int length=strlen(argv[1]); if(argc != 4) { //check that the arguments are given correctly. printf("Error: Too many arguments!\nFilename should have no spaces.\n\nUsage: %s string1 string2 filename\n",argv[0]); exit(1); } if(strlen(argv[1])!=strlen(argv[2])) { //check whether the initial and replacement strings are of same size. printf("Error: string 1 must be of equal length to string \n"); exit(1); } printf("Input text and press enter:\n"); gets(buffer); //put text from stdin into string buffer printf("Replacing all occurances of \"%s\" with \"%s\" in \"s\"\n",argv[1],argv[2],buffer); while(location=strstr(buffer,argv[1])) //replace all string1's with string2's in buffer { strncpy(location,argv[2],length); } /*write to file*/ printf("Writing to file... \n"); if ( (write_fd=creat( argv[3], O_WRONLY)) == -1 ) oops( "Cannot create file", argv[3]); if ( write( write_fd, buffer, strlen(buffer) ) != strlen(buffer) ) oops("Write error to ", argv[3]); /* close file */ if (close(write_fd) == -1 ) oops("Error closing files",""); printf("Done.\nResult:"); printf("%s\n",buffer); //output result} 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.
>>//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.
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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.
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+
![]() |
Similar Threads
- Command Line Arguments (C#)
- To fetch data through command line arguments (C++)
- HELP!!!need help with command line arguments and creating a package in java. HELP!!!! (Java)
- command line arguments (C++)
- Command-line Arguments. (C++)
Other Threads in the C Forum
- Previous Thread: Function declaration error
- Next Thread: Handling I/O redirections in my shell.
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






