problems with strcmp

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 1
Reputation: jmeson79 is an unknown quantity at this point 
Solved Threads: 0
jmeson79 jmeson79 is offline Offline
Newbie Poster

problems with strcmp

 
0
  #1
Apr 25th, 2008
i'm writing a program that takes a string and list of files and prints all the lines that contain the string. i'm having problems with my findString method. i did some debugging and it turns out when i call strcmp on strings that are the same it returns -1 or 1. does anyone know what i need to fix?

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

int inputLength = 500;

int findString(char *str, char *line) {
char *s;
s = strtok(line," ");
while (s != NULL) {
if (!strcmp(str,s))
return 1;
else
s = strtok(NULL, " ");
}
return 0;
}

int printFile(int argc, char *argv[], char *str) {
FILE *f;
char s[inputLength];
int k;
for (k = 2; k < argc; k++) {
f = fopen(argv[k],"r");
if (f) {
while (fgets(s,inputLength,f) != NULL) {
if (findString(str, s) == 1)
printf("%s %s", argv[k], s);
}
}
}
fclose(f);
return 0;
}

int main(int argc, char *argv[]) {
char * str = argv[1];
printFile(argc, argv, str);
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: IVR_Developer is an unknown quantity at this point 
Solved Threads: 2
IVR_Developer IVR_Developer is offline Offline
Newbie Poster

Re: problems with strcmp

 
0
  #2
Mar 30th, 2009
Several problems here

fgets() returns the line feed character into its argument, so you must overwrite it if you want to compare

you should use strstr() and not strcmp() since you are comparing whole lines read from your files

here is a working solution

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int inputLength = 500;
  6.  
  7. int findString(char *str, char *line) {
  8. char *s;
  9. s = strtok(line, ".");
  10. while (s != NULL) {
  11. if (!strstr(str,s))
  12. return 1;
  13. else
  14. s = strtok(NULL, " ");
  15. }
  16. return 0;
  17. }
  18.  
  19. int printFile(int argc, char *argv[], char *str) {
  20. FILE *f;
  21. char s[inputLength];
  22. int k;
  23. int i;
  24. for (k = 2; k < argc; k++) {
  25. f = fopen(argv[k],"r");
  26. if (f) {
  27. while (fgets(s, inputLength, f) && !feof(f)) {
  28. for (i=0; i<inputLength && s[i] != 0xA; i++);
  29. s[i]='\0';
  30. if (findString(str, s) == 1)
  31. printf("%s %s", argv[k], s);
  32. }
  33. }
  34. }
  35. fclose(f);
  36. return 0;
  37. }
  38.  
  39. int main(int argc, char *argv[]) {
  40. char * str = argv[1];
  41. printFile(argc, argv, str);
  42. return 0;
  43. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: IVR_Developer is an unknown quantity at this point 
Solved Threads: 2
IVR_Developer IVR_Developer is offline Offline
Newbie Poster

Re: problems with strcmp

 
0
  #3
Mar 31st, 2009
A slight improvement

Rather than search for "." with strtok(), search for (char)0x200 - the last character that is not end of line.
You can use strstr() to search for lines with common words on the same line or strcmp() to compare the whole lines. You could choose either with a switch on the command line. Meantime this works with the following invocation format

progname SearchString f1 f2

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int inputLength = 500;
  5.  
  6. int findString(char *str, char *line) {
  7. char *s;
  8. s = strtok(line, (char)0x200);
  9. printf("\nfindString(): str==<%s>", str);
  10. printf("\nfindString(): s==<%s>", s);
  11. while (s != NULL) {
  12. if (!strcmp(str,s))
  13. return 1;
  14. else
  15. s = strtok(NULL, " ");
  16. }
  17. return 0;
  18. }
  19.  
  20. int printFile(int argc, char *argv[], char *str) {
  21. FILE *f;
  22. char s[inputLength];
  23. int k;
  24. int i;
  25. for (k = 2; k < argc; k++) {
  26. f = fopen(argv[k],"r");
  27. if (f) {
  28. while (fgets(s, inputLength, f) && !feof(f)) {
  29. for (i=0; i<inputLength && s[i] != 0xA; i++);
  30. s[i]='\0';
  31. if (findString(str, s) == 1)
  32. printf("%s %s", argv[k], s);
  33. }
  34. }
  35. }
  36. fclose(f);
  37. return 0;
  38. }
  39.  
  40. int main(int argc, char *argv[]) {
  41. char * str = argv[1];
  42. printFile(argc, argv, str);
  43. return 0;
  44. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC