Thread: strtok

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    36

    strtok

    hi i have an array which is like the following which will be inputted at by the user in another pc and i wil get it through sockets....

    name:asd
    add:sdf
    married;single
    dob:11/11/12/

    i just want to find the field after married:
    i tried using strtok

    i.e. p=strtok(msg,";")

    but it will only give me values before "single" .....i just need the value after married and before dob i.e. "single"

    i am using c programming in linux
    can anyone help me

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You typically call strtok() in a loop to walk along a string extracting information.

    The first call gives you the string up to the first delimiter
    The 2nd call gives you the string up to the 2nd delimiter
    etc etc

    For only two fields, I'd probably use strchr() to find the ; and work from there.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Since you mentioned the use of sockets it might be worth mentioning that strtok isn't neccessarily thread-safe. Possibly there are compiler dependent switches/defines to make it reentrant though.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What Nyda says is a good point. It wouldn't be too hard to write your own parse function though. When I want something to parse in a very specific manner (and sscanf() won't get the job done), I just write my own parser. For what you have here, sscanf() will work just fine though.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Although, the point of threading really has nothing at all to do with sockets. You don't have to use multiple threads just because you're using sockets. There's nothing inherant to sockets that ties in to multithreading. Nothing at all. It may be more robust or fun or what not to use multiple threads, but there is no tie between the two subjects at all.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I completely agree with quzah, however I have noticed many of today's socket tutorials tend to create a new thread for a new connection. Thus its safe to say people asking simple questions such as this may very well be using such a tutorial. That said, even if you do have multiple threads, I don't see too much room for strtok() failure for this task. Additionally, I still stand by sscanf() being more appropriate for the task at hand. Its evident that the data will be in a very predictable format.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    Although, the point of threading really has nothing at all to do with sockets.
    In most non-trivial situations threads are required when working with sockets, so I thought it was worth posting a short hint. Don't see why this is now being discussed.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In most non-trivial situations threads are required when working with sockets, so I thought it was worth posting a short hint. Don't see why this is now being discussed.
    I guess you've never heard of non-blocking sockets? As for why it's being discussed, well, that's the kind of things we do around here when we're not barbecuing people for ignoring the forum rules and such. You know, discuss things...

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    I guess you've never heard of non-blocking sockets? As for why it's being discussed, well, that's the kind of things we do around here when we're not barbecuing people for ignoring the forum rules and such. You know, discuss things...

    Quzah.
    He didn't say that he was using async sockets. Even if he did it would not rule out the possibility that he was using threads along with strtok. Don't really see why I shouldn't post that or how it was against the board rules (if that's what you tried to imply).
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I wasn't suggesting you were posting outside the rules. You asked why we were discussing the topic at hand. To which I replied: That's what we do when we're not barbecuing people. You know, talk about things?

    The point was: Sockets have nothing to do with threads. You can use one without the other. They're not required for each other to operate. So when you said:
    Since you mentioned the use of sockets it might be worth mentioning that strtok isn't neccessarily thread-safe.
    I pointed out that while the two topics may be used together, they are not necessarily so. So your statement "since you mentioned sockeds ... are not thread-safe", really doesn't necessarily apply.

    One last time: Sockets != Threads.

    As to why we're discussing it, again, it's because you apparently don't see the connection. Or as in this case, the lack of connection. The two are independant topics. So just because they're using sockets, doesn't mean they're doing anything with threads, or that they ever will do anything with threads.

    Threads != Sockets

    One does not require, nor imply, the other.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    Sockets != Threads.
    Absolutely. The point is that those two topics are often related. If you google for some Linux socket examples, a whole bunch of them uses cloning, but most of the others use threads.
    That said, my choice of words might not have been that good. I tried to keep the post short as it was only a related issue and not perfectly on topic. My intention merely was to safe the OP from some really not-so-funny debugging incase he was actually using threads.
    That is why I didn't feel my post was worth this discussion, but you're of course free to disagree.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM