| | |
what is strcmp do? strcmp work?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
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?
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
C Syntax (Toggle Plain Text)
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; } }
•
•
Join Date: Aug 2009
Posts: 25
Reputation:
Solved Threads: 1
•
•
•
•
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 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?
c Syntax (Toggle Plain Text)
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.
•
•
Join Date: Sep 2009
Posts: 3
Reputation:
Solved Threads: 1
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 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....
•
•
Join Date: Aug 2009
Posts: 25
Reputation:
Solved Threads: 1
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?
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?
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:
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.
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.
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:
C Syntax (Toggle Plain Text)
a1,b1,c3, a2,b1,c3, a3,b2,c2, a2,b3,c1, a3,b2,c1,
C Syntax (Toggle Plain Text)
#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; }
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.
Manic twiddler of bits
•
•
Join Date: Aug 2009
Posts: 25
Reputation:
Solved Threads: 1
•
•
•
•
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:
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.C Syntax (Toggle Plain Text)
a1,b1,c3, a2,b1,c3, a3,b2,c2, a2,b3,c1, a3,b2,c1,
The other issues you had with your original code:C Syntax (Toggle Plain Text)
#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; }
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.
thanks a lot master...i'll learn more after this..
thanks again
![]() |
Similar Threads
- Main prog..help!! (C)
- Question about strcmp (C)
- reading a text file and searching for a sentence (C++)
- How to represent a space and a tab? (C++)
- How How How???!!!! (C++)
- get the coordenates of a word in a puzzle (C)
- removing the null terminator at the end of string (C)
- how to place a specific info from input file (C)
Other Threads in the C Forum
- Previous Thread: no one has yet to answer this question
- Next Thread: C basics - incompatible error
Views: 960 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi





