| | |
Some issues with #define "string" and the write function (unix socket related).
![]() |
I am writing a server function and an error message is defined as:
I have a function:
The problem is with the following statement:
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:
Any ideas?
Thanks.
C Syntax (Toggle Plain Text)
#define UNKNOWN "Unknown"
I have a function:
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
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.
Last edited by Ratte; Nov 2nd, 2007 at 2:36 am.
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.
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*
Re: Some issues with #define "string" and the write function (unix socket related).
0
#3 Nov 2nd, 2007
Re: Some issues with #define "string" and the write function (unix socket related).
0
#4 Nov 2nd, 2007
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.
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.
C Syntax (Toggle Plain Text)
#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.
Re: Some issues with #define "string" and the write function (unix socket related).
0
#6 Nov 2nd, 2007
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.
Re: Some issues with #define "string" and the write function (unix socket related).
0
#8 Nov 2nd, 2007
Re: Some issues with #define "string" and the write function (unix socket related).
0
#9 Nov 2nd, 2007
![]() |
Similar Threads
- JAVA Expert Help me.. Write a function that accepts an array of integers and returns (Java)
- Help using "include" inside of "echo" (PHP)
- char * = "string" and strcpy = SegFault? (C++)
- my #include <string> wont make "string" bold (C++)
- google "keyword" question (Search Engine Optimization)
- Deitel's "C++ How To Program" exercise 2.18 (C++)
Other Threads in the C Forum
- Previous Thread: Copy argv to string in C(newbie question)
- Next Thread: Need help writing a wrapper function for fork() and signal functions.
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






