pzozulka
asked on
C Programming: string compare
I'm having trouble with a method that compares two strings. Specifically I need my program to understand the difference between "http://www.csun.edu/" and "http://www.csun.edu/~steve/" (see config file below).
If I pass "http://www.csun.edu/", how can I make my program understand I'm referring to the redirect line in my config file and not the cached line.
I tried doing strcat( URL, " " ) as well as strcat( URL, "\t" ) to add an extra space or tab character at the end of my URL, but then later in my code when I do strstr( URL_ConfigFile, URL ), it returns a NULL pointer saying that a the URL string doesn't exist inside of my config file.
I think it has something to do with the additional space ( " " ) I added.
If I pass "http://www.csun.edu/", how can I make my program understand I'm referring to the redirect line in my config file and not the cached line.
I tried doing strcat( URL, " " ) as well as strcat( URL, "\t" ) to add an extra space or tab character at the end of my URL, but then later in my code when I do strstr( URL_ConfigFile, URL ), it returns a NULL pointer saying that a the URL string doesn't exist inside of my config file.
I think it has something to do with the additional space ( " " ) I added.
# Example Configuration File for deflect:
# deflect: Daemon for HTTP Egress, Filtering, Load balancing, and Caching Trivially
#
# URL-prefix Disposition Host list
http://www.csun.edu/ redirected 130.166.238.195, redwing.csun.edu
http://facebook.com/ filtered
http://www.csun.edu/~steve/ cached www.csun.edu
# Note that any URL that is not covered by any rule is transferred through the system
# without any additional processing.
What was in URL before the strcat, and how was URL_ConfigFile set?
ASKER
The URL before strcat was "http://www.csun.edu/".
The URL_ConfigFile was created using nano on a Linux machine. I tried editing that file to have tabs and spaces between the URL and the Disposition columns. I tried both ways. As of right now, I'm using tabs to separate the columns.
The URL_ConfigFile was created using nano on a Linux machine. I tried editing that file to have tabs and spaces between the URL and the Disposition columns. I tried both ways. As of right now, I'm using tabs to separate the columns.
What was in URL_ConfigFile when strstr( URL_ConfigFile, URL ) was executed?
Can you do strstr( URL_ConfigFile, "http://www.csun.edu/") and then check the following character?
Can you do strstr( URL_ConfigFile, "http://www.csun.edu/") and then check the following character?
ASKER
The config file mentioned above was read() into a buffer. I then scanned the buffer, and replaced all \r or \n with \0. This broke the file into lines where each line is a string.
Then there is a for-loop that cycles through each line, treating each line as a string.
So to answer your question, during one of the iterations of the loop:
URL_ConfigFile = "http://www.csun.edu/ redirected 130.166.238.195, redwing.csun.edu"
URL = "http://www.csun.edu/ " //tab character at the end was added with strcat()
Then there is a for-loop that cycles through each line, treating each line as a string.
So to answer your question, during one of the iterations of the loop:
URL_ConfigFile = "http://www.csun.edu/ redirected 130.166.238.195, redwing.csun.edu"
URL = "http://www.csun.edu/ " //tab character at the end was added with strcat()
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
For simplicity sake I said URL_ConfigFile, but in my code it's actually Lines[j]. See below.
So I tried strstr(Lines[j], "www.csun.edu/"), and it returns the line in the config file that says cached.
I tried comparing to "www.csun.edu/ " <-- (space char at the end), but no luck.
I also tried "www.csun.edu/ " <-- (tab char at the end), also no luck.
So I tried strstr(Lines[j], "www.csun.edu/"), and it returns the line in the config file that says cached.
I tried comparing to "www.csun.edu/ " <-- (space char at the end), but no luck.
I also tried "www.csun.edu/ " <-- (tab char at the end), also no luck.
if(strstr(Lines[j], "www.csun.edu/") != NULL) {
if(strstr(Lines[j], "redirected") != NULL) {
m = 1;
}
else if(strstr(Lines[j], "filtered") != NULL) {
m = 2;
}
else if(strstr(Lines[j], "cached") != NULL) {
m = 3;
}
}
else
m = -1;
You can't do strcat( "www.csun.edu/","\t" )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here's what I get, very strange:
Code:
http://www.csun.edu/ redirected 130.166.238.195, redwing.csun.edu
R: 9
http://facebook.com/ filtered
http://www.csun.edu/~steve/ cached www.csun.edu
Code:
fprintf(configfp,"%s\n",Lines[i]);
char *p;
if( (p=strstr(Lines[i], "www.csun.edu/")) != NULL && p[13] != '~' )
{
fprintf(configfp, "R:%c %d\n",p[13],p[13]);
}
if(strstr(Lines[i], "www.csun.edu/") != NULL) {
if(strstr(Lines[i], "redirected") != NULL) {
m = 1;
}
else if(strstr(Lines[i], "filtered") != NULL) {
m = 2;
}
else if(strstr(Lines[i], "cached") != NULL) {
m = 3;
}
}
else
m = -1;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks much, I think I know what happened. I kept on looping even after finding the correct line in my config file. Since the loop continued it would override my m variable that keeps track of this, and at the end would return the wrong thing.