| | |
problems with strcmp
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
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;
}
#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;
}
•
•
Join Date: Mar 2009
Posts: 11
Reputation:
Solved Threads: 2
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
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
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int inputLength = 500; int findString(char *str, char *line) { char *s; s = strtok(line, "."); while (s != NULL) { if (!strstr(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; int i; for (k = 2; k < argc; k++) { f = fopen(argv[k],"r"); if (f) { while (fgets(s, inputLength, f) && !feof(f)) { for (i=0; i<inputLength && s[i] != 0xA; i++); s[i]='\0'; 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; }
•
•
Join Date: Mar 2009
Posts: 11
Reputation:
Solved Threads: 2
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
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
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int inputLength = 500; int findString(char *str, char *line) { char *s; s = strtok(line, (char)0x200); printf("\nfindString(): str==<%s>", str); printf("\nfindString(): s==<%s>", s); 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; int i; for (k = 2; k < argc; k++) { f = fopen(argv[k],"r"); if (f) { while (fgets(s, inputLength, f) && !feof(f)) { for (i=0; i<inputLength && s[i] != 0xA; i++); s[i]='\0'; 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; }
![]() |
Similar Threads
- Problems from string to int array (C)
- Linked list search problems (Computer Science)
- Searching array problems (C++)
- .txt/array problems (C++)
- C-Strings & Random Integers (C++)
- FindWindow Problems (on Windows 98/ME) (C++)
- Finally Almost Finished 2 Problems!! :( (C)
- Problems of looping in saving to a text file (C)
Other Threads in the C Forum
- Previous Thread: Capturing Console Output
- Next Thread: linked list, assigning cars to random positions
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h





