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.