943,928 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3367
  • C RSS
Nov 2nd, 2007
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Ratte is offline Offline
38 posts
since Oct 2007
Nov 2nd, 2007
0

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

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 2nd, 2007
0

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

>It's like saying strlen("hello") which obviously ain't gonna work.
And why is that?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 2nd, 2007
0

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

Click to Expand / Collapse  Quote originally posted by Narue ...
>It's like saying strlen("hello") which obviously ain't gonna work.
And why is that?
Missing a semicolon.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 2nd, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ratte is offline Offline
38 posts
since Oct 2007
Nov 2nd, 2007
0

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

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 2nd, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ratte is offline Offline
38 posts
since Oct 2007
Nov 2nd, 2007
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 2nd, 2007
0

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

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC