Compare command line arguments to string in C? - c

Compare command line arguments to strings in c?
for example I want just the word "autoplay" to be the command line argument, how can i validate that this is the only word? i already have it validating for more than 1 word.
if( argc == 2 ) {
autoplay(num5);
}

You use strcmp() to compare strings; note it returns 0 on match. If you want it case insensitive then you need to use strcasecmp() instead:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
if(argc == 2 && !strcmp(argv[1], "autoplay")) {
printf("match\n");
// autoplay(num5);
return 0;
}
printf("no match\n");
}
and example run;
./a.out
no match
./a.out autoplay
match

Related

Verify if the argv 1 have a determined value [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 2 years ago.
Problem
I've this simple script to verify the argv. The problem is that if I try to input the argv 1 to "-help" it don't print the string "banner". Why it is not printing the string?
I think that is important to say that I'm noobie with the C language.
Script
#include <stdio.h>
#include <conio.h>
void main(int argc, char *argv[ ]){
int cont;
printf("argv 1 -> %s", argv[1]);
if(argv[1] == "-help"){
printf("banner");
}
if(("%s", argv[1]) == "-help"){
printf("banner");
}
//main
}
argv[1] == "-help" is comparing pointers, not the contents of strings. It will never be true because it is comparing variable region and fixed region.
("%s", argv[1]) == "-help" has the same meaning with argv[1] == "-help". , here is a comma operator.
You should use strcmp() to compare strings in C. Also don't forget to check if argv[1] has meaningful value.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[ ]){
if (argc >= 2) {
printf("argv 1 -> %s", argv[1]);
if(strcmp(argv[1], "-help") == 0){
printf("banner");
}
}
//main
}

How can i do this, Command Line Arguments in c

I am learning C, and I am in the part about the arguments: int argc, char *argv[].
I am trying to make a code that prints a result according to the argument put in console. For example:
./a.out -E
to print "Hello World!"
./a.out -S
to print "Hello World in Spanish"
I have the following code, but I still don't know how to get it.
int main(int argc, char *argv[]) {
if(argc == 1){
printf("Hello World!");
}
else if(argc > 2){
printf("Too many arguments supplied.\n");
}
else if(argv[2] == '-S'){
printf("Hola Mundo in Spanish"); //show errors
}
else {
printf("Hello, %s!!\n", argv[1]);
}
return EXIT_SUCCESS;
}
Array indexing in C is zero-based. If argc is 2, then argv[0] is the path to your executable, and argv[1] is the first argument.
The other problem you have is you cannot compare char* strings with ==, and you cannot have a single character '-S'. Yes, single-quotes is for a character, and double-quotes is for a string.
Use strcmp() from <string.h> as follows:
if (strcmp(argv[1], "-S") == 0) {
printf("Hola Mundo in Spanish\n");
}

How to determine the value of the argument entered in the terminal using C language [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 2 years ago.
I would like to write a program that performs different function based on the argument provided.
For example:
$ ./program -a //line 1
$ ./program -b //line 2
if I enter line 1 in terminal, I want it to print "Hi"
if I enter line 2 in terminal, I want it to print "Bye"
Here is my current logic, which does not work in C:
int main (int argc, char *argv[])
{
if (argv[1] == "-a"){
printf("Hi");
} else if (argv[1] == "-b")
{
printf("Bye");
}
Can anyone help me fix my code in order to achieve my objective?
Thanks in advance!
You should use strcmp() to compare strings in C.
#include <stdio.h>
#include <string.h> /* for strcmp() */
int main (int argc, char *argv[])
{
if (strcmp(argv[1], "-a") == 0){
printf("Hi");
} else if (strcmp(argv[1], "-b") == 0)
{
printf("Bye");
}
return 0;
}

Comparing C strings vs. string pointers for equality

So I am working on a project to create and use a shell. One thing that must be done is "|executable| -p |prompt| should allow the user to select an user-defined prompt. Otherwise, the default should be “257sh> ”. I wrote my code to do this however no matter what it keeps moving into the else statement. After some experimenting I know that my argv[1] == "-p" line is what is causing the issue, because without it the code works. The thing is that when I print out argv[1], it prints "-p" (assuming thats what I input). Here is my shell function.
void shellLoop(char *n)
{
char *line;
char **args;
int status;
char name = n;
do{
printf("%s>", n);
line = sysReadLine();
args = splitLine(line);
status = execute(args);
free(line);
free(args);
}while(status);
}
And here is my main function
int main(int argc, char *argv[])
{
if(argc == 3 && argv[1] == "-p"){
shellLoop(argv[2]);
}
else{
shellLoop("257sh");
}
return EXIT_SUCCESS;
}
When you do argv[1] == "-p" you compare two pointers, and two pointers that will never be the same.
To compare strings in C you use the strcmp function: strcmp(argv[1], "-p") == 0.
You can also use strncmp(char *str1,char *str2,int n) to compare the first n bytes of two strings.

How to valid 2 sticked argc in C

I'm making a very simple program code.
First, it has the option "-num" as 2nd argc. If you input anything in the 3rd argc, the program will simply say that the 3rd argc is entered.
Here are the examples of the inputs and outputs.
Input command line 1:
./test -num
Output 1
-num
Input command line 2:
./test -num AnythingHere
Output 2
-num 3rdArgcEntered
I also want the following command line with sticked argc (-num and AnythingHere are sticked together) to give the same output as Output 2:
./test -numAnythingHere
The output I wish to get is:
-num 3rdArgcEntered
But I obtained:
None
This is the source code I'm currently working on:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
if (!strcmp(argv[1], "-num"))
{
printf("-num ");
if(argc==3){
printf("3rdArgcEntered");
}
}
else
{
printf("None");
}
printf("\n");
return 0;
}
a little trash..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
/* doesnt check when args are not entered */
char *tmp = argv[1];
char buff[5];
/* copy -num, doesnt check */
memcpy(buff, tmp, 4);
buff[4] = '\0';
if (!strcmp(buff, "-num"))
{
printf("-num ");
/* larger than -num */
if(argc==3 || strlen(tmp) > 4 ){
printf("3rdArgcEntered");
}
}
else
{
printf("None");
}
printf("\n");
return 0;
}
Your program can't affect how the arguments are passed; obviously main has already been called at the beginning of main. You need to parse each argument yourself, or use a library that does it for you. For parsing them yourself, you could look at strtok or sscanf, or iterate over the characters. But a more specific library, such as getopt, is generally preferable, since it makes it easy to get your program to behave in a manner consistent with other command-line utilities.

Resources