I am writing a server function and an error message is defined as:
#define UNKNOWN "Unknown"
I have a function:
void search(int sockfd) {
FILE *file;
char arg1[MAXLINE], arg2[MAXLINE], title[MAXLINE], line[MAXLINE];
file = Fopen (PATH, "r");
while (true) {
if (Readline(sockfd, title, MAXLINE) == 0) return;
Fgets (line, MAXLINE, file);
sscanf (line, "%[^:]:%[^:]:", arg1, arg2);
while (strcasecmp(title, arg1)!=0 && !feof(file)) {
Fgets (line, MAXLINE, file);
sscanf (line, "%[^:]:%[^:]:", arg1, arg2);
}
if (strcasecmp(title, arg1)==0)
Writen(sockfd, arg2, strlen(arg2));
else
Writen(sockfd, UNKNOWN, strlen(UNKNOWN));
rewind(file);
}
Fclose(file);
}
The problem is with the following statement:
Writen(sockfd, UNKNOWN, strlen(UNKNOWN));
If I put any of the c-strings I use above in there, it will write everything correctly. With UNKNOWN, nothing happens.... it just keeps asking for more input.
By the way, this function works just as expected if implemented standalone this way:
int main () {
FILE *file;
char arg1[MAXLINE], arg2[MAXLINE], title[MAXLINE], line[MAXLINE];
file = Fopen (PATH, "r");
while (true) {
if (gets (title) == 0) return;
Fgets (line, MAXLINE, file);
sscanf (line, "%[^:]:%[^:]:", arg1, arg2);
while (strcasecmp(title, arg1)!=0 && !feof(file)) {
Fgets (line, MAXLINE, file);
sscanf (line, "%[^:]:%[^:]:", arg1, arg2);
}
if (strcasecmp(title, arg1)==0)
printf ("%s", arg2);
else {
printf("%s", UNKNOWN);
}
rewind(file);
}
Fclose(file);
exit(0);
}
Any ideas?
Thanks.