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;
}
Related
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
}
At the end of this question you will find a piece of code that I am trying to write to read a file called words.txt with the following strings:
uno dos tres cuatro cinco seis siete ocho nueve diez
The aim of the code is to be able to store the strings in a two-dimensional array with dynamic memory allocation. This means it would need to work with any file that has strings.
I would need to check:
Why the code is not working.
How can I make it so that it stores whatever number of words the file has.
Thank you very much guys!
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
int main()
{
char c, *mystring[20];
int i = 0;
FILE *fich;
setlocale(LC_CTYPE,"spanish");
identifica();
fich = fopen("words.txt", "r");
do
{
mystring[i] = malloc (20 * sizeof(char));
fscanf("%s", mystring[i]);
printf ("%s", mystring[i]);
}
while ((c=fgetc(fich))!=EOF);
return 0;
}
You forgot to pass fich to fscanf(). (This is why your code won't work)
Checking if fscanf() is successful should be performed.
You can use realloc() for dynamic re-allocation.
You should increment i for storeing all strings.
Maximum length of string to read should be specified to avoid buffer overrun.
Try this:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
int main()
{
char **mystring = NULL;
int i = 0;
FILE *fich;
setlocale(LC_CTYPE,"spanish");
identifica();
fich = fopen("words.txt", "r");
for (;;)
{
char* next = malloc (20 * sizeof(char));
if (fscanf(fich, "%19s", next) == 1)
{
printf ("%s", next);
mystring = realloc(mystring, sizeof(*mystring) * (i + 1));
mystring[i] = next;
i++;
}
else
{
free(next);
break;
}
}
return 0;
}
When I read from file I want to check the word do not contains the number, for example, 2sum or 23b2 but is OK if the file read the mathematical operation like
sum = x + 5
I try to put inside the if statement [a-zA-Z]* put it does not work.
This part of my code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int charClass;
char lexeme [100];
void addChar();
void getChar();
void getNonBlank();
int lex();
int main() {
if ((in_fp = fopen("/Users/Desktop/ff.txt", "r")) == NULL)
printf("ERROR - cannot open front.in \n");
else {
getChar();
do {
if(strcmp(lexeme, "[a-zA-Z]*") == 0){
printf("error");
break;
}
lex();
} while (nextToken != EOF);
}
}
Regarding:
if(strcmp(lexeme, "[a-zA-Z]*") == 0){
This is comparing the char array [a-zA-Z]* to the array pointed at by lexeme
There are two ways to approach this problem.
First, use an regular expression, which requires several complexities that will just confuse you (or me)
Second, iterate over the expression pointed to by: lexeme as in:
for( size_t i=0; lexeme[i]; i++ )
{
if( isalpha( lexeme[i] )
{
....
}
}
The functions in ctype.h work on indivdual int values and when referencing: lexeme[i] that single character gets prompted to an 'int'
when the input is yes in status .the string s does not seem to be able to get printed.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char name[20],sta[3];
scanf("%s",&name);
scanf("%s",&sta);
if((strcmp("Yes",sta)==0)||(strcmp("yes",sta)==0))
printf("Mrs.%s",name);
if((strcmp("No",sta)==0)||(strcmp("no",sta)==0))
printf("Ms.%s",name);
return 0;
}
I have improved your code based on suggestions in comments.
#include <stdio.h>
#include <string.h>
int main() {
char name[20] = {0},
sta[4] = {0};
scanf("%s", name);
scanf("%s", sta);
if ((strcmp("Yes", sta) == 0) || (strcmp("yes", sta) == 0))
printf("Mrs.%s\n", name);
if ((strcmp("No", sta) == 0) || (strcmp("no", sta) == 0))
printf("Ms.%s\n", name);
return 0;
}
First created sta of size 4 to allow 3 characters of "Yes" to fit in it along with '\0' terminator.
scanf() doesn't require & operator for character strings.
Initialized character array to '\0' (equivalent to ASCII 0).
Removed not required header files and improved formatting a bit.
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))