• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: string help required.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string help required.


  • Subject: Re: string help required.
  • From: Phil Brayshaw <email@hidden>
  • Date: Tue, 06 Sep 2005 18:29:55 +0100

Not sure what this has to XCode, but I here goes:

strncmp doesn't search one string for occurrences of another string. It matches up to the *first* N characters of a string.

That is strncmp( "Here I am", "Here you were", 4 ) will match (since the first 4 characters match). This won't: strncmp( "Here I am", "am", 2 ) - since the *first* 2 characters don't match.

Try strstr() or another C library function ( http://www.cplusplus.com/ref/cstring/strstr.html ), or consider using stl string classes (if you're using C++) as it'll make your code easier to read and less error prone.

ph.

D. Walsh wrote:
While this may be basic for most, I can't seem to make it work and it's probably because I don't know enough.

I'm trying to search a line buffer for a string and as long as the string appears at the start of the line buffer it matches but anywhere else in the line it doesn't match.

Can someone help me with this search pattern so that it locates the pattern regardless of position in the line buffer?

Here's what I have for code:


    sprintf((char *)patternToSearch,"%s", (char *)my_search_string);

    while (fgets(buffer, sizeof(buffer), fp)) {
    sprintf((char *)patternToSearch,"%s", (char *)my_search_string);
        if (strncmp((char *)patternToSearch, buffer, strlen((char *)patternToSearch))) {
            /* are we writing to a temp file? */
            if (file_cmd == 4) {
                fputs(buffer, temp);
            }
        } else {
            matched = true;
            /* are we writing to a temp file? if yes then comment out the matched line */
            if (file_cmd == 4) {
                fprintf(temp, "#");
                fputs(buffer, temp);
            }
        }
    }
    if (is_verbose) {
        if (matched) {
            printf("Found match for : %s\n", patternToSearch);
        } else {
            printf("No match for : %s\n", patternToSearch);
        }
    }


_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: string help required.
      • From: "D. Walsh" <email@hidden>
    • Re: string help required.
      • From: "D. Walsh" <email@hidden>
References: 
 >string help required. (From: "D. Walsh" <email@hidden>)

  • Prev by Date: Re: Undefined STL symbols in xcode...
  • Next by Date: Re: Don't invoke this method when build configurations are enabled
  • Previous by thread: string help required.
  • Next by thread: Re: string help required.
  • Index(es):
    • Date
    • Thread