• 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: "D. Walsh" <email@hidden>
  • Date: Tue, 6 Sep 2005 14:22:44 -0400
  • Mta-interface: amavisd-new-2.3.3 (20050822) at daleenterprise.com


On Sep 06, 2005, at 01:29 PM, Phil Brayshaw wrote:

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

Not much but it is an avenue of people who have the knowledge and experience,

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.

Checkout the API and have a working solution, thanks 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

References: 
 >string help required. (From: "D. Walsh" <email@hidden>)
 >Re: string help required. (From: Phil Brayshaw <email@hidden>)

  • Prev by Date: Re: string help required.
  • Next by Date: build settings and source paths
  • Previous by thread: Re: string help required.
  • Next by thread: ObjC++ and .mm vs .cpp
  • Index(es):
    • Date
    • Thread