Some issues with #define "string" and the write function (unix socket related).

Reply

Join Date: Oct 2007
Posts: 35
Reputation: Ratte is an unknown quantity at this point 
Solved Threads: 0
Ratte's Avatar
Ratte Ratte is offline Offline
Light Poster

Some issues with #define "string" and the write function (unix socket related).

 
0
  #1
Nov 2nd, 2007
I am writing a server function and an error message is defined as:

  1. #define UNKNOWN "Unknown"

I have a function:

  1. void search(int sockfd) {
  2.  
  3. FILE *file;
  4. char arg1[MAXLINE], arg2[MAXLINE], title[MAXLINE], line[MAXLINE];
  5.  
  6. file = Fopen (PATH, "r");
  7.  
  8. while (true) {
  9.  
  10. if (Readline(sockfd, title, MAXLINE) == 0) return;
  11.  
  12. Fgets (line, MAXLINE, file);
  13. sscanf (line, "%[^:]:%[^:]:", arg1, arg2);
  14.  
  15. while (strcasecmp(title, arg1)!=0 && !feof(file)) {
  16.  
  17. Fgets (line, MAXLINE, file);
  18. sscanf (line, "%[^:]:%[^:]:", arg1, arg2);
  19.  
  20. }
  21.  
  22. if (strcasecmp(title, arg1)==0)
  23. Writen(sockfd, arg2, strlen(arg2));
  24.  
  25. else
  26. Writen(sockfd, UNKNOWN, strlen(UNKNOWN));
  27.  
  28. rewind(file);
  29.  
  30. }
  31.  
  32. Fclose(file);
  33.  
  34. }

The problem is with the following statement:

  1. 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:

  1. int main () {
  2.  
  3. FILE *file;
  4. char arg1[MAXLINE], arg2[MAXLINE], title[MAXLINE], line[MAXLINE];
  5.  
  6. file = Fopen (PATH, "r");
  7.  
  8. while (true) {
  9.  
  10. if (gets (title) == 0) return;
  11.  
  12. Fgets (line, MAXLINE, file);
  13. sscanf (line, "%[^:]:%[^:]:", arg1, arg2);
  14.  
  15. while (strcasecmp(title, arg1)!=0 && !feof(file)) {
  16.  
  17. Fgets (line, MAXLINE, file);
  18. sscanf (line, "%[^:]:%[^:]:", arg1, arg2);
  19.  
  20. }
  21.  
  22. if (strcasecmp(title, arg1)==0)
  23. printf ("%s", arg2);
  24.  
  25. else {
  26. printf("%s", UNKNOWN);
  27. }
  28.  
  29. rewind(file);
  30.  
  31. }
  32.  
  33. Fclose(file);
  34.  
  35. exit(0);
  36. }

Any ideas?
Thanks.
Last edited by Ratte; Nov 2nd, 2007 at 2:36 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Some issues with #define "string" and the write function (unix socket related).

 
0
  #2
Nov 2nd, 2007
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.
Last edited by iamthwee; Nov 2nd, 2007 at 4:32 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 725
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Some issues with #define "string" and the write function (unix socket related).

 
0
  #3
Nov 2nd, 2007
>It's like saying strlen("hello") which obviously ain't gonna work.
And why is that?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Some issues with #define "string" and the write function (unix socket related).

 
0
  #4
Nov 2nd, 2007
Originally Posted by Narue View Post
>It's like saying strlen("hello") which obviously ain't gonna work.
And why is that?
Missing a semicolon.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 35
Reputation: Ratte is an unknown quantity at this point 
Solved Threads: 0
Ratte's Avatar
Ratte Ratte is offline Offline
Light Poster

Re: Some issues with #define "string" and the write function (unix socket related).

 
0
  #5
Nov 2nd, 2007
Actually, you can do strlen(UNKNOWN). Give it a try and see that it works.

  1. #define UNKNOWN "Unknown"
  2. #include <stdio.h>
  3.  
  4.  
  5. int main ()
  6. {
  7.  
  8. printf ("%d", strlen(UNKNOWN));
  9.  
  10. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Some issues with #define "string" and the write function (unix socket related).

 
0
  #6
Nov 2nd, 2007
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.
Last edited by iamthwee; Nov 2nd, 2007 at 4:54 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 35
Reputation: Ratte is an unknown quantity at this point 
Solved Threads: 0
Ratte's Avatar
Ratte Ratte is offline Offline
Light Poster

Re: Some issues with #define "string" and the write function (unix socket related).

 
0
  #7
Nov 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 725
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Some issues with #define "string" and the write function (unix socket related).

 
0
  #8
Nov 2nd, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Some issues with #define "string" and the write function (unix socket related).

 
0
  #9
Nov 2nd, 2007
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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