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 6598357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:20:10+00:00 2026-05-25T18:20:10+00:00

i am trying to parse a text file saved in doc dir below show

  • 0

i am trying to parse a text file saved in doc dir below show is the code for it

NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDirPath=[filePaths objectAtIndex:0];
NSString *filePath=[docDirPath stringByAppendingPathComponent:@"SKU.txt"];
NSError *error;
NSString *fileContents=[NSString stringWithContentsOfFile:filePath];
NSLog(@"fileContents---%@",fileContents);   
if(!fileContents)
NSLog(@"error in reading file----%@",error);
NSArray *values=[fileContents componentsSeparatedByString:@"\n"];
NSLog(@"values-----%@",values);

NSMutableArray *parsedValues=[[NSMutableArray alloc]init];
for(int i=0;i<[values count];i++){
    NSString *lineStr=[values objectAtIndex:i];
    NSLog(@"linestr---%@",lineStr);
    NSMutableDictionary *valuesDic=[[NSMutableDictionary alloc]init];
    NSArray *seperatedValues=[[NSArray alloc]init];
    seperatedValues=[lineStr componentsSeparatedByString:@","];
    NSLog(@"seperatedvalues---%@",seperatedValues);
    [valuesDic setObject:seperatedValues forKey:[seperatedValues objectAtIndex:0]];
    NSLog(@"valuesDic---%@",valuesDic);
    [parsedValues addObject:valuesDic];
    [seperatedValues release];
    [valuesDic release];
}
NSLog(@"parsedValues----%@",parsedValues);
NSMutableDictionary *result;
result=[parsedValues objectAtIndex:1];
NSLog(@"res----%@",[result objectForKey:@"WALM-FT"]);

The problem what i am facing is when i try to print lineStr ie the data of the text file it is printing as a single string so i could not able to get the contents in line by line way please help me solve this issue.

  • 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-25T18:20:11+00:00Added an answer on May 25, 2026 at 6:20 pm

    Instead use:

    - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
    

    it covers several different newline characters.

    Example:

    NSArray *values = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    for (NSString *lineStr in values) {
        // Parsing code here
    }
    

    ALso seperatedValues is over released. First one is created with alloc init, then on the next line it is replaced by the method componentsSeparatedByString. So the first one od lost without being released, that is a leak. Later the seperatedValues created by componentsSeparatedByString is released but it is already auto released by componentsSeparatedByString to that is an over release;

    Solve all the retain/release/autorelease problem with ARC (Automatic Reference Counting).

    Here is a version that uses convenience methods and omits over release:

    NSArray *values = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    for (NSString *lineStr in values) {
        NSArray *seperatedValues = [lineStr componentsSeparatedByString:@","];
        NSString *key = [seperatedValues objectAtIndex:0];
        NSDictionary *valuesDic = [NSDictionary dictionaryWithObject:seperatedValues forKey:key];
        [parsedValues addObject:valuesDic];
    }
    NSLog(@"parsedValues---%@",parsedValues);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse text-based file attachments (txt, doc, etc...). However, I can't seem
I'm trying to parse a text file into sentences ending in periods, but names
I'm trying to parse some data in a fixed format text file where each
I'm trying to parse a text file that has a heading and the body.
I am trying to parse a text file using regex. I am using notepad++
I am trying to parse a plain text file using Tika but getting inconsistent
I have a text file which I'm trying to parse. The file looks like
I am trying to parse a table in the form of a text file
I am trying to parse a text file that is of the form: A
I'm trying to parse a text file word by word and I need to

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.