943,895 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4383
  • C++ RSS
Nov 23rd, 2008
0

Read from file, delimiter is ;

Expand Post »
I am developing an interactive client and server system which can response to a typical Human's Interaction. This program is run in unix enviornment.

There's a Q&A text file stored, in this format.
[Question];[Reply]
How old are you?;I Am 1 year old.

basically the delimiter is a ';'.
Is there any function in c i can call to make some checking?

When the client side sends a query "How old are you" to the server, server is suppose to reply "I am 1 year old"

I know i could use strtok to use ';' as my delimiter. But how do i extract out the answer "I am 1 year old" if it matches?

C++ Syntax (Toggle Plain Text)
  1. void reply(int sock)
  2.  
  3. {
  4.  
  5. int n, i; //declare variables
  6.  
  7. char buffer[256]; //to store input from client
  8.  
  9. //get message from client
  10.  
  11. bzero(buffer,256);
  12.  
  13. n = read(sock,buffer,255);
  14.  
  15.  
  16.  
  17. //prompt error if cannot read
  18.  
  19. if (n < 0) error("ERROR reading from socket");
  20.  
  21. //change all char to lower
  22. for(i=0; buffer[i]; i++)
  23. buffer[i]=tolower(buffer[i]);
  24.  
  25. //Need to check the text file here. to get a reply.
  26. // If nothing matches, reply with "Sorry i do not have the answer."
  27.  
  28. //send message back to client
  29. n = write(sock, buffer, 18);
  30.  
  31. if (n < 0) error("ERROR writing to socket")
  32.  
  33.  
  34. }
Last edited by AcidG3rm5; Nov 23rd, 2008 at 3:24 am.
Similar Threads
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008
Nov 23rd, 2008
0

Re: Read from file, delimiter is ;

Have you ever read about strtok?

http://www.cplusplus.com/reference/c...ng/strtok.html

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Nov 23rd, 2008
0

Re: Read from file, delimiter is ;

i've read about that. actualyl am trying to get getline(a,b,';').

Any moderators around? Can help to shift this thread to c instead? Realised that i posted in the wrong section as i'm doing this program in c
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008

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:
Previous Thread in C++ Forum Timeline: ifstream and eof
Next Thread in C++ Forum Timeline: generating an 'infinite' number





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


Follow us on Twitter


© 2011 DaniWeb® LLC