i have loaded the string data from file to double array.this is example:
const_array[1][]=a1,b1,c3,
const_array[2][]=a2,b1,c3,
const_array[3][]=a3,b2,c2,
const_array[4][]=a2,b3,c1,
const_array[5][]=a3,b2,c1,

those data need to be compared to this string:
append_test_data.this is also an array and it will be changed randomly by d program.the prob is,my prog just compare to the last data in array,const_array[5][]=a3,b2,c1,
i use this code to compare.

for(int n=1;n<=m;n++)
     {
       if(strcmp(append_test_data,const_array[n]) == 0)
                {
                   printf("Append data is =%s\n",append_test_data);
                   printf("constraint is found=%s\n",const_array[n]);

                   getch();
                  exit(0);

              }
     }

my question is,how is strcmp works?is it compare to the lenght of the string or the string itself?why dose it compare to the last data in array?

Ya.. It compares the strings...

If u say append_test_data is a String array i dont find an index append_test_data[index] while comparing?

If u say append_test_data is a String array i dont find an index append_test_data[index] while comparing?

its bcoz i declare it before.do i need to write append_test_data [MAX_LENGTH] everytime i want to use it?

Compare two strings.

is it include this kind of string?
a1,b1,c3 compare to a2,b1,c1

if dat so, why is my code just compare the last last in array?
do u have any comment about my code?
im really stuck wif this thing.....:(

is it "a1","b1" ,"c1" or "a1,b1,c1"?
It is a simple concept of array if u have to compare every value u have to give both the entities an index ..[max_length].U have to modify accordingly if the two arrays are not of the same length.It is probably finding only the last value because append_test_data without an index is just one value.

if its multi dimensional array then u have to keep track of the counts ur comparing like for example
for(i=0;i<MAX;i++) //this is a case in which string have same count
if(!strcmp(SzString,SzString2)) { //do stuff};

also arrays starts at 0 count not 1

is it "a1","b1" ,"c1" or "a1,b1,c1"?
It is a simple concept of array if u have to compare every value u have to give both the entities an index ..[max_length].U have to modify accordingly if the two arrays are not of the same length.It is probably finding only the last value because append_test_data without an index is just one value.

its "a1,b1,c1".
i use const_array as double array so that everytime i just query the row,not the column.
and append_test _data is just a string array.
do u meant with this declaration :
char append_test_data [MAX_LENGTH]="";
i use "" in this array.does it effect?

its "a1,b1,c1".
i use const_array as double array so that everytime i just query the row,not the column.
and append_test _data is just a string array.
do u meant with this declaration :
char append_test_data [MAX_LENGTH]="";
i use "" in this array.does it effect?

normally append_test_data[MAX_LENGTH]="";
is just \0 stored in it do you want to make user each time get string from him through the loop and then compare it ? also note that int n=1 that will start from 2nd array count not first since arrays start from 0 could you also post full code to see whats wrong with it

i think something like this would work

for(int n=0;n<=sizeof SzStrings/SzStrings[0];n++)
     {
       Putrand(append_test_data);
       if(strcmp(append_test_data, SzStrings[n]) == 0)
                {
                   printf("Append data is =%s\n",append_test_data);
                   printf("constraint is found=%s\n", SzStrings[n]);

                   getch();
                  return 0;

              }
     }

I like the above discussion. Really. I can really learn from all of you, my masters.

normally append_test_data[MAX_LENGTH]="";
is just \0 stored in it do you want to make user each time get string from him through the loop and then compare it ? also note that int n=1 that will start from 2nd array count not first since arrays start from 0 could you also post full code to see whats wrong with it

i repeat my prob,
i have an array with 5 row of data and each row has these data:
const_array[0][]=a1,b1,c3,
const_array[1][]=a2,b1,c3,
const_array[2][]=a3,b2,c2,
const_array[3][]=a2,b3,c1,
const_array[4][]=a3,b2,c1,

then i want each of them are compared to this :
char append_test_data[100]="";
for note,the data in this array is always randomly changed but only 1 data each time.so,this means,the const_array just compare to 1 data in append_test_data in a time.

example:
append_test_data =a2,b1,c3, =>the program cant compare them
but if append_test_data=a3,b2,c1, =>only program can compare them.
whats wrong with my program?

if ((in_const = fopen(const_file, "r+")) == NULL)
    {
      fprintf(stderr, "Cannot open constraint file.\n");
      exit(0);
    }
    char const_array[100][100];

    int m=0 ;
    while(!feof(in_const)& m <=100)

    {
       fgets(const_array[m],100,in_const);        
       m++;
    }

 for(int n=0;n<=m;n++)
     {
      if(strcmp(const_array[n],append_test_data) == 0)

          {
              printf("Append data is =%s\n",append_test_data);
              printf("constraint is found=%s\n",const_array[n]);

              getch();
              exit(0);
          }
     }

actually,
i call the file and load to an array in the same function.maybe it effect sumthing.

Hi...
I read your information as per that i have some good information on strcmp which i want to share with you....The following error appears: 'strcmp' : cannot convert parameter 1
from 'char' to 'const char *'. I've already tried using single
quotations. the header file
only contains the struct contents. The whole program is part of an
example found in my course work. Does strcmp only compare two sets of
strings or can it be used to determine the end of the string as well?

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include "program2v.h"

using namespace std;

int main()
{


crecord *string1, *string2, t_str;

*string1 = t_str;

/// the error while( strcmp(*string1->customername, "\0"))
{
*string2 = *string1 + 1;
while( strcmp(*string2->customername, '\0'))
{
if(strcmp( *string1->customername, *string2->customername) > 0 )
{
t_str = *string1;
*string1 = *string2;
*string2 = t_str;

}
string2++;
}
string1++;
}


return 0;
}...
Thanks for sharing the post....

i found that,the lenght of the strings are different.
strlen (const_array[m])=10,while
strlen (append_test_data)=9.
the funny is,the last data of const_array is 9.thats why its only compile the last data in that array because only the last data has the same lenght with append_test_data.

a1,b1,c3, =strlen (const_array[0])=10
a2,b1,c3, =strlen (const_array[1])=10
a3,b2,c2, =strlen (const_array[2])=10
a2,b3,c1, =strlen (const_array[3])=10
a3,b2,c1, =strlen (const_array[4])=10
a3,b1,c2, =strlen (const_array[5])=9

wow!!!!!

anyone knows how its happens?

it is used to compare the string letter by letter..if they are same then it return the value zero otherwise the difference in the ascii values in the first letter of the two string which are different.

For the OP - if I'm understanding your issue correctly, you are still experiencing problems getting the strcmp() function to return the fact that two strings are equal.

This is happening because the fgets() function stores the newline character into the buffer and then you are comparing this buffer to a string which you think is the same but it doesn't have the '\n' character. You need to overwrite the '\n' character with a NULL.

Because I'm sick of seeing this thread going nowhere (and I've had a good day at work), here's some code for you to try. Here's the "constraint.txt" file I used:

a1,b1,c3,
a2,b1,c3,
a3,b2,c2,
a2,b3,c1,
a3,b2,c1,

Here's the code that reads the above file into the const_array and then tries to find a string that is equal to append_test_data.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {

    FILE *fp = NULL;
    char const_array[100][100];
    char append_test_data[100]="a2,b3,c1,";
    int ctr= 0, i = 0;
    char *p = NULL;

    // open constraints file
    if ((fp = fopen("constraint.txt", "r")) == NULL) {
        printf("Cannot open constraint file.\n");
        return EXIT_FAILURE;
    }

    // read file and load each line of text into array
    // don't forget to overwrite the '\n' that fgets reads into the buffer
    while (fgets(const_array[ctr], 100, fp) != NULL) {
        if ((p = strchr(const_array[ctr], '\n'))) {
            *p = '\0';
        }
        ctr++;
    }

    // check constraint against append data
    for (i = 0; i < ctr; i++) {
        if (strcmp(const_array[i], append_test_data) == 0) {
            printf("Append data is =%s\n", append_test_data);
            printf("constraint is found=%s\n", const_array[i]);
        }
    }

    fclose(fp);
    return 0;
}

The other issues you had with your original code:

1) You can't declare a variable in the condition statement of a for loop in C.

2) Your use of the feof() function is all wrong. You need to study up on this. In any case it has been removed for the above code snippet.

You also need to start learning how to look up function descriptions in the C function library documentation - if you had done this, you would've been aware of the fgets() function storing the newline character in the buffer.

you can also use strncmp or use strchr to remove new line after each time fgets reads the string

For the OP - if I'm understanding your issue correctly, you are still experiencing problems getting the strcmp() function to return the fact that two strings are equal.

This is happening because the fgets() function stores the newline character into the buffer and then you are comparing this buffer to a string which you think is the same but it doesn't have the '\n' character. You need to overwrite the '\n' character with a NULL.

Because I'm sick of seeing this thread going nowhere (and I've had a good day at work), here's some code for you to try. Here's the "constraint.txt" file I used:

a1,b1,c3,
a2,b1,c3,
a3,b2,c2,
a2,b3,c1,
a3,b2,c1,

Here's the code that reads the above file into the const_array and then tries to find a string that is equal to append_test_data.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {

    FILE *fp = NULL;
    char const_array[100][100];
    char append_test_data[100]="a2,b3,c1,";
    int ctr= 0, i = 0;
    char *p = NULL;

    // open constraints file
    if ((fp = fopen("constraint.txt", "r")) == NULL) {
        printf("Cannot open constraint file.\n");
        return EXIT_FAILURE;
    }

    // read file and load each line of text into array
    // don't forget to overwrite the '\n' that fgets reads into the buffer
    while (fgets(const_array[ctr], 100, fp) != NULL) {
        if ((p = strchr(const_array[ctr], '\n'))) {
            *p = '\0';
        }
        ctr++;
    }

    // check constraint against append data
    for (i = 0; i < ctr; i++) {
        if (strcmp(const_array[i], append_test_data) == 0) {
            printf("Append data is =%s\n", append_test_data);
            printf("constraint is found=%s\n", const_array[i]);
        }
    }

    fclose(fp);
    return 0;
}

The other issues you had with your original code:

1) You can't declare a variable in the condition statement of a for loop in C.

2) Your use of the feof() function is all wrong. You need to study up on this. In any case it has been removed for the above code snippet.

You also need to start learning how to look up function descriptions in the C function library documentation - if you had done this, you would've been aware of the fgets() function storing the newline character in the buffer.

U r the man!!!!
thanks a lot master...i'll learn more after this..
thanks again

Thanks for everyone who follow my thread.
i learnt 2 things here:
1- fget count '\n' as character
2-strcmp requires the same lenght of string to be compared

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.