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.

Member Avatar for iamthwee

most probably because you can't do strlen(UNKNOWN). It's like saying strlen("hello") which obviously ain't gonna work.

Why not just initialise it properly? If you don't know how, you're way out of your depths.

Also Fgets why the capitalization? gets why?
feof Again why? I could go on.

>It's like saying strlen("hello") which obviously ain't gonna work.
And why is that?

Member Avatar for iamthwee

>It's like saying strlen("hello") which obviously ain't gonna work.
And why is that?

Missing a semicolon.

Actually, you can do strlen(UNKNOWN). Give it a try and see that it works.

#define UNKNOWN "Unknown"
#include <stdio.h>


int main ()
{

printf ("%d", strlen(UNKNOWN));
    
}

The above program prints 7.

I do it with #DEFINE because that is how the instructor wants it. If I did it otherwise, I would get penalized. It already happened before.

Fgets is because I use a wrapper function for fgets (also required). This code is all based on stuff in a UNIX socket programming book.

I just found out what the problem was. The client uses Readline (sort of an fgets) for reading the server output... so everything that I send from the server has to be \n terminated. Yes, I know. I also can't beleive how much time I wasted on something this trivial.

Member Avatar for iamthwee

I dunno, I don't really use c. I don't know what you mean by add a newline at the string. But glad you sorted it.

fgets reads until x (size of buffer) amount of characters are read OR a newline character is encountered. In my case, the buffer was quite large, so the client's fgets was waiting till either the buffer was full, or a newline character was sent.... and since I wasn't sending a \n, it kept waiting. That was the problem.

>Missing a semicolon.
Do you practice being obtuse?

>I dunno, I don't really use c.
Then maybe you shouldn't be trying to answer questions about C.

Member Avatar for iamthwee

:'(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.