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

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
}

Related

Compare command line arguments to string in 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

How to solve segmentation fault in c?

When I was making my program I got an error type of thing called Segmentation Fault.
#include <cs50.h>
#include <string.h>
#include <stdio.h>
int main(int argc, string argv[])
{
char i = strlen(argv[2]);
if (argc == 2)
{
printf("%i %s %hhd", argc, argv[2], i);
}
}
I run this program using these commands
make substitution
and then
./substitution abcdefghijklmnopqrstuvwxyz
In this we have to add a 26 word key which in the above line is a to z.
Please help if you know to solve
If you invoke your program with:
./substitution abcdefghijklmnopqrstuvwxyz
argc will be 2, and argv will be an array of 3 pointers:
argv[0] points to the string ./substitution, argv[1] points to the string abcdefghijklmnopqrstuvwxyz, and argv[2] is NULL. If you attempt to compute the length of NULL by calling strlen(argv[2]), that is an error. You must not pass NULL to strlen. I think your error is simply mis-indexing the argv array. Arrays in C are zero based. If you want to compute the length of the first argument, you want to work with argv[1], not argv[2]:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
int rc = EXIT_FAILURE;
if( argc > 1 ){
size_t i = strlen(argv[1]);
printf("%i %s %zd\n", argc, argv[1], i);
rc = EXIT_SUCCESS;
}
return rc;
}
You got hit with segfault because the number of required arguments specified was done poorly. The main problem is here:
if (argc == 2)
The number of actual arguments passed by the user is equal to the number of required arguments minus 11. It should be:
int main(int argc, char *argv[]) {
if (argc != 3) {
// Number of arguments are either greater or lesser than 3
// Handle the error
}
// Ok...
}
You can safely use the arguments now.
1. The calling command for the program, for example ./a.out is also counted as an argument (argv[0]).

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;
}

Check if two files are the same [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 3 years ago.
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
int main()
{
int status = 0;
FILE * fPointer;
FILE * gPointer;
fPointer = fopen("file1.txt", "r");
gPointer = fopen("file2.txt", "r");
char singleLine[150];
char secondLine[150];
while(fgets(singleLine,150,fPointer)!=NULL && fgets(secondLine,150,gPoi$
{
//fgets(singleLine,150,fPointer);
//fgets(secondLine,150,gPointer);
printf("singleLine: %s\n",singleLine);
printf("secondLine: %s\n",secondLine);
if (singleLine != secondLine)
{
status = 1;
}
}
printf("final status: %d\n", status);
if (status == 0)
{
printf("same\n");
}
else if (status == 1)
{
printf("not same\n");
}
fclose(fPointer);
fclose(gPointer);
return 0;
}
The contents of both files are "hello" and "hello". But for some reason the output I get is
singleLine: hello
secondLine: hello
final status: 1
which equals "not the same".
I checked by printing what singleLine and secondLine are at each iteration and they are the same.
What am I doing wrong?
The following doesn't quite work as you think it does:
if (singleLine != secondLine)
That is because singleLine and secondLine are arrays (treated as strings). Equality/inequality operators in C, when used for arrays, simply check whether the two arrays reside at the same address in memory (i.e. are the same variable). Which in your case are not, so your if statement is always true.
Since you are treating both arrays as strings, the correct function to use is strcmp or strncmp, both defined in <string.h>. This is the standard way of performing string comparisons in C (hence the name of the functions).
Your if statement, in this case should be:
if (strcmp(singleLine, secondLine) != 0)
{
status = 1;
}

While loop not stopping when arguments are equal [duplicate]

This question already has answers here:
How to compare strings in an "if" statement? [duplicate]
(5 answers)
Closed 8 years ago.
I am running this program with with ./crack 50yoN9fp966dU
50yoN9fp966dU is crimson encrypted. which is on the word list. My program is as follow:
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc > 2)
{
printf("Invalid Argument \n");
return 1;
}
else
{
FILE *fp1;
fp1 = fopen("/usr/share/dict/words", "r");
char line[9];
while (fgets(line, 9, fp1) != NULL)
{
char *EncryptLine1;
char *EncryptLine2;
printf("%s", line);
EncryptLine1 = crypt(line, "50");
if(argv[1] == EncryptLine1)
{
printf("%s \n", line);
}
EncryptLine2 = crypt(line, "HA");
if(argv[1] == EncryptLine2)
{
printf("%s \n", EncryptLine2);
}
}
}
}
If I add a printf("%s", EncryptLine1), I see the argv[1], i.e 50yoN9fp966dU, but the loop continue and does not print the answer.
You are doing pointer comparison instead of contents (pointed data) comparison.
Change this:
if (argv[1] == EncryptLine1)
To this:
if (strcmp(argv[1],EncryptLine1) == 0)
And this:
if (argv[1] == EncryptLine2)
To this:
if (strcmp(argv[1],EncryptLine2) == 0)
You have some problems in your code:
You blithely assume argc will never be smaller than 2. Check for unequal 2 instead of bigger two in your first condition.
Anyway, if you return out of an if-block, using else and doing deeper nesting is really superfluous.
Strings cannot be compared with ==, use strcmp.:
if( ! strcmp(argv[1], EncryptLine1))
You need to add a break to break out of the loop or a return to leave the function in your conditional block after printing success, if you want to end the loop there.
if( ! strcmp(argv[1], EncryptLine1)) {
printf("%s \n", line);
break;
}
BTW: Why don't you reuse EncryptLine1 (not that you need any temporary at all there)?
argv[1], EncryptLine1 and EncryptLine2 are all char*s. operator== on two char*s simply checks to see if they are pointing to the same memory location. What you want is to compare the contents of the strings they represent. So, the ifs should look like this:
if(!strcmp(argv[1], EncryptLine1))

Resources