Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6351195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:57:18+00:00 2026-05-24T21:57:18+00:00

This has given me quite a big headache. For whatever reason, when I use

  • 0

This has given me quite a big headache. For whatever reason, when I use this code, the if statement always evaluates to false:

while(!feof(file))
{
    NSString *line = [self readNSString:file];
    NSLog(@"%@", line);
    NSLog(@"%@", search);
    NSRange textRange;
    textRange =[line rangeOfString:search];

    if(textRange.location != NSNotFound)
    {
        NSString *result = [line substringFromIndex:NSMaxRange([line rangeOfString:search])];
        resultView.text = result;
    }
    else
    {
        resultView.text = @"Not found";
    }
}

When the functions execute, the two NSLogs tell me that the “line” and “search” strings are what they should be, so then why does the if statement always evaluate to false? I must be missing something simple, having another set of eyes would be great. Thanks

edit: (function “readNSString”)

- (NSString*)readNSString:(FILE*) file
{
    char buffer[300];

    NSMutableString *result = [NSMutableString stringWithCapacity:256];
    int read;
    do
    {
        if(fscanf(file, "%299[^\n]%n%*c", buffer, &read) == 1)
            [result appendFormat:@"%s", buffer];
        else
            break;
    } while(r == 299);

    return result;
}

edit 2:

search is set with a call to the first function, with an NSString* variable as a parameter, like this:

NSString *textFieldText = [[NSString alloc]
                              initWithFormat:@"%@", textField.text];

[self readFile:textFieldText];

edit 3 (NSLogs output)

line: Germany Italy France

search: Italy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:57:20+00:00Added an answer on May 24, 2026 at 9:57 pm

    I think that you are using the rangeOfString and the NSNotFound etc. correctly, so the problem is possibly to do with the creation of the string from the data read from the file using the appendFormat:@"%s".

    I suspect there may be an encoding issue between your two string formats – I would investigate whether the “%s” encodes the null terminated C string properly into the same format as a unicode NSString with the appropriate encoding.

    Try hard coding the value you are getting from the readNSString function as a string literal in code just for testing and see if that comparison works, if so this would tend to indicate it probably is something to do with the encoding of the string created from the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This are few of the fundamental database questions which has always given me trouble.
Given that django-nonrel has got JOINs working, does this mean we have M2M fields
It's quite possible a question like this has been asked before, but I can't
Wondering if anyone out there has ran into this before.... I'd like to use
I'm hoping someone has run into this sort of problem before, and can give
This has been a problem that I haven't been able to figure out for
This has been an adventure. I started with the looping duplicate query located in
This has been driving me crazy for a few days. Why doesn't the following
This has happened to me 3 times now, and I am wondering if anyone
This has got to be something I just missed, but how do I add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.