Read from file, delimiter is ;

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 16
Reputation: AcidG3rm5 has a little shameless behaviour in the past 
Solved Threads: 0
AcidG3rm5 AcidG3rm5 is offline Offline
Newbie Poster

Read from file, delimiter is ;

 
0
  #1
Nov 23rd, 2008
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?

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Read from file, delimiter is ;

 
0
  #2
Nov 23rd, 2008
Have you ever read about strtok?

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

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 16
Reputation: AcidG3rm5 has a little shameless behaviour in the past 
Solved Threads: 0
AcidG3rm5 AcidG3rm5 is offline Offline
Newbie Poster

Re: Read from file, delimiter is ;

 
0
  #3
Nov 23rd, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC