strcspn return incorrect results

This is a discussion on strcspn return incorrect results within the C Programming forums, part of the General Programming Boards category; hello, i'm trying to locate the occurence in a string of a comparison string using srcspn, but it never returns ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    strcspn return incorrect results

    hello,
    i'm trying to locate the occurence in a string of a comparison string using srcspn, but it never returns correct results,

    Code:
    char acc[] = "Accept-Encoding:";
    int loc = strcspn(tmp, acc);
    where tmp is a char array containing the http request:

    GET <request> HTTP/1.1
    Host: <host>
    User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-us,en;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 115
    Proxy-Connection: keep-alive
    Cookie: __utma=212617111.289542702.1278157741.1278907954.1 278912107.14; __utmz=212617111.1278907954.13.3.utmcsr=enrol.rmit .edu.au|utmccn=(referral)|utmcmd=referral|utmcct=/eol/AcadHistoryList.asp
    Cache-Control: max-age=0


    basically what i want to do is remove the Accept-Encoding portion of the http request so my server doesn't receive gzipped files, but strcspn for "Accept-Encoding" returns 1, am i doing something wrong? please help,
    regards,

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    It sounds like you want strstr(), not strcspn().

    Once you've found a pointer to a substring, subtract the original string to find the offset.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    Duplicate post.
    Last edited by cas; 10-07-2010 at 07:44 PM. Reason: Dupe.. I blame my browser!

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    hi,
    thanks, strstr works fine to locate the encoding header then memset to 0 the length of the encoding string but that only works because i know what sort of content i'm receiving from the remote server (i'm writing a proxy as an exercise, btw), and how long the header field is, but how could i make this more generic? and not rely on say, the length of a string "gzip,deflate"?

    edit: the reason i'm not passing the entire request is because i need to cache the contents of the remote server and with the Accept-Encoding header there i get garbage (i.e. gipped content).
    Last edited by qualia; 10-07-2010 at 09:19 PM. Reason: requisite information not forthcoming

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    one last thing, obviously i'm not understanding strcspn,
    the example i seen on cplusplus.com leads me to believe that strcspn for my purpose should return something like 300 or so, but as i said it returns only one. the first letter of the search string is G, not even remotely like "Accept-Encoding"

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    thanks, strstr works fine to locate the encoding header then memset to 0 the length of the encoding string but that only works because i know what sort of content i'm receiving from the remote server (i'm writing a proxy as an exercise, btw), and how long the header field is, but how could i make this more generic? and not rely on say, the length of a string "gzip,deflate"?
    You want to split the header up into lines, not treat it as one chunk of text. Then you can easily modify, pass through, or ignore any headers.
    the example i seen on cplusplus.com leads me to believe that strcspn for my purpose should return something like 300 or so, but as i said it returns only one. the first letter of the search string is G, not even remotely like "Accept-Encoding"
    strcspn() does not search for a substring. The second argument is a list of characters (which must be a string, yes, but it is not a search string). strcspn() will search through the first argument until it finds any one of the characters in the second argument; the return value is the index of the first character.

    Since your acc array contains the letter E, and the second character in your header is E, it stops processing when it sees it, and returns its index, which is 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21